Exception of type 'System.IO.IOException' occurred in mscorlib.dll - The process cannot access the file because it is being used by another process
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:
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:
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.
G droid
Updated on May 31, 2020Comments
-
G droid about 4 years
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
-
nvoigt about 9 yearsFile.AppendText will create the file if it does not exist. You don't need that
if
. And while I agree generally, that building the string once and writing it to file once is better, if it's one line per pixel of an image it might be too large for doing so. -
Reg Edit about 9 yearshow does this answer the question? Neither of these points will cause the error. "You are trying to recreate the file for every loop iteration." No, look again, there is an
if
statement:if (!File.Exists(path))
. "And you try to outsmart the API, that already knows when it has to create a new file": true but harmless and will not cause an error. -
Reg Edit about 9 years@nvoigt it's a good point about the amount of text being cached; I've updated my answer to mention it. The file existence check is harmless though. When showing a suggested change, I prefer to leave the original code in place as much as possible to make it clear what point I'm making.