Access Denied trying to read file with StreamReader [modified]
-
I'm trying to read a text file that just happens to be a log file for a service, and getting the following error. "System.IO.IOException: The process cannot access the file 'D:\SomeFolder\Logs\Funsv.log' because it is being used by another process." The exception makes it very clear what the problem is, but I can't find any options in StreamReader that allow me read-only access to a file, even when it's being used by another process. Does anyone know how this can be done? -- modified at 8:02 Friday 18th August, 2006
-
I'm trying to read a text file that just happens to be a log file for a service, and getting the following error. "System.IO.IOException: The process cannot access the file 'D:\SomeFolder\Logs\Funsv.log' because it is being used by another process." The exception makes it very clear what the problem is, but I can't find any options in StreamReader that allow me read-only access to a file, even when it's being used by another process. Does anyone know how this can be done? -- modified at 8:02 Friday 18th August, 2006
If the process that has the file open for write is using DenyShareRead or DennyShareAll to open it, then you can't open it for reading at all until the service closes the file.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
If the process that has the file open for write is using DenyShareRead or DennyShareAll to open it, then you can't open it for reading at all until the service closes the file.
Dave Kreskowiak Microsoft MVP - Visual Basic
Ouch. In that scenario, would it be possible to read the file using WinAPI?
-
I'm trying to read a text file that just happens to be a log file for a service, and getting the following error. "System.IO.IOException: The process cannot access the file 'D:\SomeFolder\Logs\Funsv.log' because it is being used by another process." The exception makes it very clear what the problem is, but I can't find any options in StreamReader that allow me read-only access to a file, even when it's being used by another process. Does anyone know how this can be done? -- modified at 8:02 Friday 18th August, 2006
I solved this problem by using a FileStream instead of a StreamReader. The constructor of the FileStream allows you to set FileAccess and FileShare which avoids the access problems. The FileStream doesn't allow you to read line-by-line nicely, but I happen to have a class that I re-use often which acts like a TextReader but uses a FileStream in the background.
-
Ouch. In that scenario, would it be possible to read the file using WinAPI?
Matt Casto wrote:
would it be possible to read the file using WinAPI?
No, because it's the Win API that is underlying the Stream classes.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
I solved this problem by using a FileStream instead of a StreamReader. The constructor of the FileStream allows you to set FileAccess and FileShare which avoids the access problems. The FileStream doesn't allow you to read line-by-line nicely, but I happen to have a class that I re-use often which acts like a TextReader but uses a FileStream in the background.
You can wrap the FileStream in a StreamReader object like this:
FileStream fs = New FileStream("filepath", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = New StreamReader(fs);You'll then have all the advantages of the StreamReader, with the access flexibility of the FileStream. -- modified at 17:47 Friday 18th August, 2006
Dave Kreskowiak Microsoft MVP - Visual Basic