【问题标题】:C# StreamReader Holding up the process even if I closeC# StreamReader 即使我关闭也会阻止进程
【发布时间】:2015-10-13 01:57:05
【问题描述】:

我目前收到以下错误:

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 语句开始,而不是手动调用Close()。否则,如果抛出异常,您不会调用Close。此外,它看起来像是一个实例变量 - 几乎可以肯定,将其设为局部变量会更好......
  • 哦,但它无法复制的文件不是您正在阅读的文件。我怀疑您仍在运行该应用程序的一个实例,这就是为什么您无法构建需要复制到同一位置的更新版本的原因。
  • 我没有实例。那是我的问题。我不能使用using,因为我们的教授没有教过,我知道这很荒谬,因为这不是我们在现实世界中最好的案例方法。这就是让我厌烦的地方。我已经阅读了 c# msdn 或其他内容,但我需要使用 Close,但我会尝试关闭计算机并重新启动。我确实不小心从 while EOF 出现了一个无限循环,并且没有重新启动 stream_line 读取
  • 如果您不能使用using,则将所有内容包装在try-catch 中,并在finally 块中调用Close
  • “我没有启动实例” - 好吧,检查任务管理器。但问题不在于 StreamReader,因为那不是同一个文件......

标签: c# file-io streamreader


【解决方案1】:

我已经多次遇到这个问题,它说“该进程无法访问文件 'bin\Debug\Some.exe',因为它正被另一个进程使用。”

通常您应该清理解决方案并重新构建它,它会解决问题。

Build -> Clean Solution

然后

Build -> Rebuild Solution

如果这不能解决问题。比您应该手动从 bin 文件夹中删除 Soldier.exe 并再次构建解决方案

【讨论】:

  • 是的,我已经清理和重建了大约 10 次。接下来我可能会删除 exe 文件
  • 好吧,我只需要重新启动计算机,肯定有一个应用程序打开但没有显示,因为我在意识到并启动它之前在 while 循环中犯了错误。所以这一定是我的错误。
【解决方案2】:

首先,我建议使用 using.这样,即使您最终遇到异常,文件也会被关闭(并且阅读器会被处理)。 另一件事是,如果 ReadLine 到达流的末尾,则返回 null。 但 StreamReader 中有一个有用的属性:EndOfStream。当您到达流的末尾时,情况确实如此。

所以我建议这样的代码:

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;
            }
        }
    }

}

【讨论】:

  • 显然您没有阅读上面的 cmets。我很想使用正确的方法,例如using,但遗憾的是我不能,我的教授会给我打分。他以前对我这样做过,因为他使用了一些愚蠢的东西,例如在 C++ 中使用 EOF。或 while 循环。等等。
【解决方案3】:

由于“托管进程”,在 Visual Studio 上进行调试时经常会发生这种情况。 vshost.exe 是否在您正在运行的进程中?

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

  1. 在“项目”菜单上,单击“属性”。
  2. 单击“调试”选项卡。
  3. 清除启用 Visual Studio 托管进程复选框。

如果 vshost.exe 仍然保留您的文件,这可能会解决您的问题。

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 2015-09-22
    • 2017-11-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2015-05-05
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多