好的,我在标题中不断收到错误,我实际上不确定为什么。您可以在我向文件添加内容的方法中看到代码的第一部分,另一部分是我从文件中读取时。
好吧,我试图在谷歌搜索时关闭文件,但我只是不断收到错误。
if (File.Exists("important.txt"))
{
using (StreamWriter writer = File.AppendText("important.txt"))
writer.Write("rn" + name + "," + socialid + "," + town + "," + numsold);
}
else
{
using (StreamWriter writer = new StreamWriter("important.txt"))
{
writer.Write(name + "," + socialid + "," + town + "," + numsold);
}
System.IO.StreamReader file = new System.IO.StreamReader("important.txt");
while ((line = file.ReadLine()) != null)
{
string[] array = line.Split(',');
int num1 = Int32.Parse(array[3]);
if(num1 < 50)
{
str2 = line.Replace(",", " ");
System.Console.WriteLine(str2);
counter++;
}
}
file.Close();
我什至不确定是什么原因导致此问题发生,并且在谷歌上搜索修复程序没有结果。
请尝试以下操作:
string path = @"C:\important.txt";
if (File.Exists(path))
File.AppendAllText(path,"rn" + name + "," + socialid + "," + town + "," + numsold);
else
File.AppendAllText(path, name + "," + socialid + "," + town + "," + numsold);
string[] lines = File.ReadAllLines(path);
foreach(var line in lines)
{
string[] array = line.Split(',');
int num1 = Int32.Parse(array[3]);
if(num1 < 50)
{
str2 = line.Replace(",", " ");
System.Console.WriteLine(str2);
counter++;
}
}