Sign in to follow this  
Subconscious

[.net] System.IO troubles

Recommended Posts

Subconscious    136
I need to access a text file for reading, but it is in use by windows. Notepad can read the file just fine, but no matter what options I try to open a file with in C#, it throws an IOException saying the file is in use. I've been using File.Open(path, FileMode.Open, FileAccess.Read) and I've played with the FileShare option too but nothing helped. Any suggestions?

Share this post


Link to post
Share on other sites
Nypyren    12074
That's odd. If Notepad can open the file, you can probably open it with at least read access...

Try the FileStream constructor(s) and see if any of them can open it properly.

Share this post


Link to post
Share on other sites
Subconscious    136
I've tried all combinations of FileStream constructors, I've tried File.OpenRead(), FileInfo.OpenRead(), and File.Open with all combinations of paramters... nothing I can think of will open this file even for read only purposes.

After much googling I've found several other people with my exact same problem trying to read .log files used by windows. In each of the other forums I've seen this thread come up, it goes a few pages with nothing working and then someone suggests the ugly solution of using FileInfo.CopyTo() to make a copy of the file then reading the copied file and deleting it later.

If notepad can read the file, surely there has to be another way.

Share this post


Link to post
Share on other sites
Nypyren    12074
Just curious:

- What kind of file is it? Is it a system-related file, or your own file that no other programs are accessing?
- Is it on a local hard drive, a CD or other removable media, or on a network share?
- Are you running your app from a network share?
- Are you specifying the absolute or relative name of the file (i.e. is the Open operation looking in the right folder?)

- Have you tried using an OpenFileDialog to select and open the file?

[Edit] My bad, I missed the part where you said you were trying to open the windows .log files. Which one in particular are you opening?

[Edit 2] I tried opening the C:\windows\system32\config\software.log file (the one that my FileSystemWatcher picks up as being modified all the time) with Notepad, and it popped up the "it's being used by another process". Same result with the C# binary editor utility that I wrote.

Share this post


Link to post
Share on other sites
Subconscious    136
specifically I'm trying to open C:\WINDOWS\System32\LogFiles\W3SVC1\ex051006.log, basically it's the logfile for web activity today and still in use by windows.

[edit]
I can open every other log file in the directory, but just not the one for today.

Share this post


Link to post
Share on other sites
APCJim    121
This worked on an old mainframe:

Copy the file to another filename and open that new file. Make sure you delete it when done an remember it isn't being updated.

Share this post


Link to post
Share on other sites
Justaddwater    100
I just opened each log in there and had no problems...



DirectoryInfo di = new DirectoryInfo(@"C:\WINDOWS\System32\LogFiles\W3SVC1");
StreamReader sr;

foreach (FileInfo fi in di.GetFiles())
{
sr = new StreamReader(fi.FullName);
MessageBox.Show(sr.ReadToEnd());
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this