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

G droid picture G droid · May 30, 2015 · Viewed 33k times · Source

Probably there are number of questions like this one on SO but none of them has been able to fix my issue. I am creating a text file programatically and witting the text in to it using this code:

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

I want to write the value of each pixel in the text file. It works for few values and then suddenly stops throwing an above exception.

Moreover i have checked the security property of the file and made sure that i have the full control over the file. I am not able to figure it out.

Any suggestions to fix this would be really appreciated

Answer

Reg Edit picture Reg Edit · May 30, 2015

Your program should work. Therefore the file is being opened by another process, as the message says. Unless you are doing something yourself to cause this, very likely the culprit is Windows Search.

To stop Windows Search from interfering with your program, stop the Windows Search service. Enter the command services.msc from the Start menu or a command prompt:

enter image description here

Select the row for Windows Search, and stop the service using the toolbar button or right-click menu.

If you wish, you may also disable the service, to prevent it from starting up again when you restart your computer. The property settings are available by double-clicking or the right-click menu:

enter image description here

Regarding your code, consider using a StringBuilder to store the text in your loop, and then write the text to the file once at the end, rather than hitting the file system a zillion times:

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());

EDIT: Note that if you do use this kind of approach, the string could get very large, so you may want to optimize it, for example by writing to file each time the string reaches a certain length instead of only once at the end.