我目前遇到了这些错误:
Error 18 Could not copy "obj\Debug\SoldierApp.exe" to "bin\Debug\SoldierApp.exe". Exceeded retry count of 10. Failed.
Error 19 Unable to copy file "obj\Debug\SoldierApp.exe" to "bin\Debug\SoldierApp.exe". The process cannot access the file 'bin\Debug\SoldierApp.exe' because it is being used by another process.
我猜测我的StreamReader
在某个地方没有正确关闭。尽管我知道我已经关闭了它。这是我的代码:
public Form1()
{
InitializeComponent();
if(File.Exists("soldier.csv"))
{
sr = new StreamReader("soldier.csv");
string stream_line;
int intCommaIndex1 = 0;
int intCommaIndex2;
string strSubStringSect;
stream_line = sr.ReadLine();
while(stream_line != EOF) //EOF is a constant
{
intCommaIndex1 = stream_line.IndexOf(",", intCommaIndex1);
intCommaIndex2 = stream_line.IndexOf(",", intCommaIndex1);
strSubStringSect = stream_line.Substring(intCommaIndex1, intCommaIndex2);
richTextBox1.Text = richTextBox1.Text + " " + strSubStringSect;
intCommaIndex1 = intCommaIndex2;
stream_line = sr.ReadLine();
}
sr.Close(); //closed here!
}
}
我对C#
文件IO还比较陌生,但是看起来好像一切都正确了?有什么建议可以解决这些错误吗?
using
,因为我们的教授还没有教过那个,真是荒谬,因为在现实世界中这不是我们最佳的方法。这就是让我恼火的地方。我已经阅读了C#的MSDN或其他资料,但我需要使用Close
,但我会尝试关闭电脑然后重新启动。我之前在EOF
的循环中出现了无限循环,并且没有重新初始化stream_line
的读取。 - EasyBBusing
,则将所有内容放在try-catch
中,并在finally
块中调用Close
。 - Arturo Torres Sánchezif
语句内的整个内容可以替换为richTextBox1.Text = String.Join(" ", File.ReadAllLines("soldier.txt").TakeWhile(line => line != EOF).Select(line => line.Split(',')[1]));
。也许你可以尝试这样做,看看问题是否解决了?这样你就会知道是否是你的代码有问题。 - Enigmativity