Hacker Forums

the process cannot access the file because it is being used by another process.


by The Uni-Hacker on May.13, 2007, under Misc

This message appeared while I was trying to read a text file that was locked. I was very frustrated because notepad and other text editors could open it no problem, but my simple StreamReader couldn’t open it. The code below was giving me this error and I had to do a little tweaking and fiddling to get it to work.

Broken Code

StreamReader fr = new StreamReader(this.eveFile);

Working Code
FileStream fs = new FileStream(this.eveFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
StreamReader sr = new StreamReader(fs);

Apparently you have to open the file specifically for Read access, thus the “FileAccess.Read”. It appears that StreamReader opens in ReadWrite no matter what, and you can’t change the FileAccess in StreamReader. No problem though as this is only one more line of code.

:, , ,
Click the .NET+C#+Programming+visual+studio+ to view the picture in full size.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...