'System.IO.IOException' 类型的异常发生在 mscorlib.dll - 进程无法访问该文件,因为它正被另一个进程使用

Exception of type 'System.IO.IOException' occurred in mscorlib.dll - The process cannot access the file because it is being used by another process

SO 上可能有很多类似的问题,但其中 none 已经能够解决我的问题。我正在以编程方式创建一个文本文件,并使用以下代码将文本写入其中:

 for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    string path = @"E:\Example.txt";


                    Color pixel = image.GetPixel(x, y);
                    if (!File.Exists(path))
                    {
                      //  File.Create(path);
                       File.Create(path).Dispose();
                        using (TextWriter tw = new StreamWriter(path))
                        {
                            tw.WriteLine("Value at" + x + "" + y + "" + "is:" +pixel);
                            tw.Close();
                        }

                    }

                    else if (File.Exists(path))
                    {
                        File.AppendAllText(path,
                   "Value at"+"" + x + "" + y + "" + "is:" +""+ pixel + Environment.NewLine);
                        //using (TextWriter tw = new StreamWriter(path))
                        //{
                        //    tw.WriteLine("The next line!");
                        //    tw.Close();
                        //}
                    }
                    if (pixel.R == keywordBytes[0] && pixel.G == keywordBytes[1] && pixel.B == keywordBytes[2])
                    {

                        firstMatchingBytePos = new Point(x, y);
                        KeywordFound(keyword, firstMatchingBytePos);
                    }
                }
            } 

我想在文本文件中写入每个像素的值。它适用于少数值,然后突然停止抛出上述异常。

此外,我已经检查了文件的安全性 属性 并确保我拥有对该文件的完全控制权。我想不通。

如有任何解决此问题的建议,我们将不胜感激

您正在尝试为每次循环迭代重新创建文件。而你试图智取 API,它已经知道什么时候必须创建一个新文件。停止那个:

string path = @"E:\Example.txt";

for (int y = 0; y < image.Height; y++)
{
    for (int x = 0; x < image.Width; x++)
    {
        Color pixel = image.GetPixel(x, y);

        // this line is all you need to append a line to an existing, OR NEW file
        File.AppendText(path, "\nValue at" + x + "" + y + "" + "is:" + pixel);

        if (pixel.R == keywordBytes[0] && pixel.G == keywordBytes[1] && pixel.B == keywordBytes[2])
        {
            firstMatchingBytePos = new Point(x, y);
            KeywordFound(keyword, firstMatchingBytePos);
        }
    }
} 

您的程序应该可以运行。因此,如消息所述,该文件正在被另一个进程打开。除非是您自己造成的,否则罪魁祸首很可能是 Windows 搜索。

要阻止 Windows 搜索干扰您的程序,请停止 Windows 搜索服务。从“开始”菜单或命令提示符输入命令 services.msc

Select Windows 行搜索,并使用工具栏按钮或右键单击菜单停止服务。

如果您愿意,您也可以禁用该服务,以防止它在您重新启动计算机时再次启动。 属性设置可通过双击或右键菜单获得:

关于您的代码,请考虑使用 StringBuilder 将文本存储在循环中,然后在最后将文本写入文件一次,而不是无数次地访问文件系统:

StringBuilder sb = new StringBuilder();

for (int y = 0; y < image.Height; y++)
{
    for (int x = 0; x < image.Width; x++)
    {
        Color pixel = image.GetPixel(x, y);

        sb.AppendLine("Value at" + x + "" + y + "" + "is:" + pixel);

        if (pixel.R == keywordBytes[0] && pixel.G == keywordBytes[1] && pixel.B == keywordBytes[2])
        {
            firstMatchingBytePos = new Point(x, y);
            KeywordFound(keyword, firstMatchingBytePos);
        }
    }
}

string path = @"E:\Example.txt";

if (!File.Exists(path))
{
    File.Create(path);
}

File.AppendAllText(path, sb.ToString());

编辑: 请注意,如果您使用这种方法,字符串可能会变得非常大,因此您可能需要对其进行优化,例如通过写入每个文件字符串达到一定长度的时间,而不是最后一次。

尝试在 File.AppendAllText

之后添加 .Close()