C# StreamReader关闭后仍阻塞进程

3

我目前遇到了这些错误:

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还比较陌生,但是看起来好像一切都正确了?有什么建议可以解决这些错误吗?


2
哦,但是无法复制的文件并不是你正在阅读的文件。我怀疑你仍然有一个应用程序实例在运行,这就是为什么你无法构建一个需要复制到同一位置的更新版本。 - Jon Skeet
我没有任何实例。这就是我的问题。我不能使用using,因为我们的教授还没有教过那个,真是荒谬,因为在现实世界中这不是我们最佳的方法。这就是让我恼火的地方。我已经阅读了C#的MSDN或其他资料,但我需要使用Close,但我会尝试关闭电脑然后重新启动。我之前在EOF的循环中出现了无限循环,并且没有重新初始化stream_line的读取。 - EasyBB
如果无法使用 using,则将所有内容放在 try-catch 中,并在 finally 块中调用 Close - Arturo Torres Sánchez
1
我没有任何实例了——好吧,在任务管理器中检查一下。但问题不在于StreamReader,因为那不是同一个文件... - Jon Skeet
if语句内的整个内容可以替换为richTextBox1.Text = String.Join(" ", File.ReadAllLines("soldier.txt").TakeWhile(line => line != EOF).Select(line => line.Split(',')[1]));。也许你可以尝试这样做,看看问题是否解决了?这样你就会知道是否是你的代码有问题。 - Enigmativity
显示剩余5条评论
3个回答

1
我已经遇到过这个问题好几次了,它显示“由于另一个进程正在使用,无法访问文件 'bin\Debug\Some.exe'。”
通常情况下,您应该“清理解决方案并重新构建”,问题就会得到解决。
Build -> Clean Solution

然后

Build -> Rebuild Solution

如果这个方法没有解决问题,那么你应该手动从bin文件夹中删除Soldier.exe,并重新构建解决方案。

1
是的,我已经清理和重建了大约10次了。也许我会选择删除exe文件。 - EasyBB
只需重新启动电脑就行了,可能是因为我在之前的while循环中犯了一个错误,导致某个应用程序没有显示出来。所以那肯定是我的错误。 - EasyBB

0

在使用Visual Studio进行调试时,经常会因为"hosting process"(托管进程)出现这种情况。你的运行进程中是否有vshost.exe?

关闭Visual Studio,重新打开你的解决方案,然后尝试以下操作:

  1. 点击项目菜单,选择属性。
  2. 点击调试选项卡。
  3. 取消选中启用Visual Studio托管进程复选框。

如果vshost.exe仍然占用你的文件,这个方法可能能解决你的问题。


0
首先,我建议使用using关键字。这样,即使出现异常,文件也会被关闭(读取器也会被释放)。 另外,如果ReadLine方法到达流的末尾,它会返回null。 但是StreamReader类有一个很有用的属性:EndOfStream。当你到达流的末尾时,它的值为true。
所以,我建议使用以下代码:
public Form1()
{
    if(File.Exists("soldier.csv"))
    {
        InitializeComponent();
        using (StreamReader sr = new StreamReader("soldier.csv"))
        {
            string stream_line;
            int intCommaIndex1 = 0;
            int intCommaIndex2;
            string strSubStringSect;
            while (!sr.EndOfStream)
            {
                stream_line = sr.ReadLine();
                intCommaIndex1 = stream_line.IndexOf(",", intCommaIndex1);
                intCommaIndex2 = stream_line.IndexOf(",", intCommaIndex1);
                strSubStringSect = stream_line.Substring(intCommaIndex1, intCommaIndex2);
                richTextBox1.Text = richTextBox1.Text + " " + strSubStringSect;
                intCommaIndex1 = intCommaIndex2;
            }
        }
    }

}

显然你没有读上面的评论。我很想使用正确的方法,比如using,但可惜我不能这样做,否则我的教授会扣分。他以前就因为我使用了一些愚蠢的东西,比如在C++中使用EOF或者while循环而扣过我的分。等等。 - EasyBB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接