public static void Main()
{
FileStream fs = null;
NameValueCollection dataCol = new NameValueCollection();
string sourceFile = @"C:\data.txt";
if (!File.Exists(sourceFile))
{
Console.WriteLine("Source File Not Found!");
Console.ReadLine();
return;
}
//Opens the source file.
fs = new FileStream(sourceFile, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string line;
//A value is read from the file.
while (sr.Peek() > -1)
{
line = sr.ReadLine();
if (line.Contains('='))
{
string[] data = line.Split('=');
dataCol.Add(data[0].Trim(), data[1].Trim());
}
}
sr.Close();
fs.Close();
//create a new txt file for each property
foreach (string key in dataCol.AllKeys)
{
//check directory
if (!Directory.Exists(@"C:\test"))
Directory.CreateDirectory(@"C:\test");
//open the new file, if not exists, create it
StreamWriter newFile = File.AppendText(string.Format(@"C:\test\{0}.txt", key));
//get all value with the same property
string[] keyValue = dataCol[key].Split(',');
//write values to the file
foreach (string value in keyValue)
newFile.WriteLine(value);
newFile.Close();
}
}
[[it] 本帖最后由 slokra 于 2008-6-25 09:59 编辑 [/it]]