Jump to content
  • Advertisement
Sign in to follow this  
Subconscious

[.net] System.IO troubles

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!