|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Something I don't quite understand about "file in use by another process" exceptions.....I was just playing around with my log files and tried to open a log file programmatically that was considered "in use" (I got an in-use exception). The file is being used by my debug writer and I wanted to read lines from the file on one of my forms. It's a simple text file by the way. Now obviously the exception I get from the code trying to open the file for reading looks like this: The process cannot access the file 'C:\Documents and Settings\mymachine\Application Data\mycomapny\myproduct\log.txt' because it is being used by another process. What I don't understand is that I can double click the file and have it open up in notepad.exe. I don't get an "in use" exception from Explorer, so what is notepad.exe doing that I cannot being able to open the file? I notice that you can File.Copy the file while it's in-use and this strikes me as one way I could get to read a file already in use by the system (copy and then open and read the copy). Can anyone shed any light on Windows inner workings in this respect? Thanks, Robin what is your code for opening the file?
Did you open for read only? Show quoteHide quote "Robinson" <toomuchspamhaspassed@myinboxtoomuchtoooften.com> wrote in message news:egdtn3$5jq$1$8300dec7@news.demon.co.uk... > Hi, > > I was just playing around with my log files and tried to open a log file > programmatically that was considered "in use" (I got an in-use exception). > The file is being used by my debug writer and I wanted to read lines from > the file on one of my forms. It's a simple text file by the way. Now > obviously the exception I get from the code trying to open the file for > reading looks like this: > > The process cannot access the file 'C:\Documents and > Settings\mymachine\Application Data\mycomapny\myproduct\log.txt' because it > is being used by another process. > > What I don't understand is that I can double click the file and have it open > up in notepad.exe. I don't get an "in use" exception from Explorer, so what > is notepad.exe doing that I cannot being able to open the file? I notice > that you can File.Copy the file while it's in-use and this strikes me as one > way I could get to read a file already in use by the system (copy and then > open and read the copy). Can anyone shed any light on Windows inner > workings in this respect? > > Thanks, > > > > Robin > > "GS" <gsmsnews.microsoft.co***@msnews.Nomail.com> wrote in message Hi,news:eWPB0G86GHA.4644@TK2MSFTNGP04.phx.gbl... > what is your code for opening the file? > Did you open for read only? > Yes, I tried File.ReadAll (as a test), a StreamReader and File.Open ( for reading ). All of these threw the exception, but File.Copy did not. Robin That was not the question.
The question was whether or not you specified that you are opening the file in read-only mode when opening the file for reading. Show quoteHide quote "Robinson" <toomuchspamhaspassed@myinboxtoomuchtoooften.com> wrote in message news:egduqq$79n$1$8300dec7@news.demon.co.uk... > > "GS" <gsmsnews.microsoft.co***@msnews.Nomail.com> wrote in message > news:eWPB0G86GHA.4644@TK2MSFTNGP04.phx.gbl... >> what is your code for opening the file? >> Did you open for read only? >> > > Hi, > > Yes, I tried File.ReadAll (as a test), a StreamReader and File.Open ( for > reading ). All of these threw the exception, but File.Copy did not. > > > Robin > "Marina Levit [MVP]" <someone@nospam.com> wrote in message File.ReadAllLines does not have an overload to specify for read-only, just news:eclAoQ86GHA.4592@TK2MSFTNGP04.phx.gbl... > That was not the question. > > The question was whether or not you specified that you are opening the > file in read-only mode when opening the file for reading. > path and encoding. File.Open, has Create, Append, CreateNew, Open, OpenOrCreate, Truncate (I chose Open) Dim theStream As New StreamReader, has overloads for encoding and auto-detection of encoding, but not a "read only" flag. However in the latter case, I'm sure it's read only in any case because it's a streamreader, not a streamreaderorwriter. Did I miss something here? Robin Hello Robinson,
I've not validated your assertions, but the FileStream class does provide an Open method that takes a (I believe) FileMode enum (flag) value. -Boo Show quoteHide quote > "Marina Levit [MVP]" <someone@nospam.com> wrote in message > news:eclAoQ86GHA.4592@TK2MSFTNGP04.phx.gbl... > >> That was not the question. >> >> The question was whether or not you specified that you are opening >> the file in read-only mode when opening the file for reading. >> > File.ReadAllLines does not have an overload to specify for read-only, > just path and encoding. > > File.Open, has Create, Append, CreateNew, Open, OpenOrCreate, Truncate > (I chose Open) > > Dim theStream As New StreamReader, has overloads for encoding and > auto-detection of encoding, but not a "read only" flag. However in > the latter case, I'm sure it's read only in any case because it's a > streamreader, not a streamreaderorwriter. > > Did I miss something here? > > Robin > "GhostInAK" <p***@paco.net> wrote in message Thanks for all above replies, I did indeed miss an overload when searching news:be1391bf1beff8c8b9d07d43785c@news.microsoft.com... > Hello Robinson, > > I've not validated your assertions, but the FileStream class does provide > an Open method that takes a (I believe) FileMode enum (flag) value. > > -Boo the documentation. The correct way of doing it is: theStream = File.Open(Application.UserAppDataPath + "\log.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite) Robinson wrote:
> File.ReadAllLines does not have an overload to specify for read-only, just Hi Robin,> path and encoding. > > File.Open, has Create, Append, CreateNew, Open, OpenOrCreate, Truncate (I > chose Open) > > Dim theStream As New StreamReader, has overloads for encoding and > auto-detection of encoding, but not a "read only" flag. However in the > latter case, I'm sure it's read only in any case because it's a > streamreader, not a streamreaderorwriter. You mentioned trying to understand the inner workings. All of these ..NET framework methods are just indirect means to call the Windows API. And when opening a file directly with the API, in addition to the filename it expects three parameters: FileMode: Create, Append, CreateNew, Open, OpenOrCreate, Truncate FileAccess: Read, Write, ReadWrite FileShare: Delete, Inheritable, None, Read, ReadWrite, Write I don't know my way around the .NET framework yet as well as the API, but it seems that the problem is that the methods you've tried so far assume FileShare=None, and do not allow you to override it to ReadWrite, which is what you really need. But I do know of one that does (there may be more): Dim f As New System.IO.FileStream("c:\log.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
Locating Windows Application Installation Keys
Drop your pants and grab your ankles..its VISTA! Seeking info on httpweblistener.... Bad practice with Enumerations.... How to find my information vb.net 2003 ... Global Formatting ? Reflection - Delay Signing Error - HELP Merge VB.NET and C# MDI Child start position &H201 |
|||||||||||||||||||||||