|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FileShare parameter in filestream creationHi All,
Can anyone explain the differences between FileShare.None and FileShare.ReadWrite in filestreams using C#.NET windows application? Thanks, Murali. <muralidhar***@hotmail.com> wrote in message
news:1154775802.522701.32070@p79g2000cwp.googlegroups.com... FileShare.None declares that you are unwilling to share access to the file > Hi All, > > Can anyone explain the differences between FileShare.None and > FileShare.ReadWrite in filestreams using C#.NET windows application? > with anyone else. You will open the file exclusively and if any other process attempts to open the file, an error will be raised. FileShare.ReadWrite declares that you are willing to share both read an write access to the file with other users. David Thanks Mr.David.
I have a C# windows legacy framework. In my framework, Iam able to access a file using FileShare.None from two different applications. But the same fails when I try to access from same application multiple times. The code that I had written is: FileStream stream = new FileStream(_MessageFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None); int byteCount = (int)stream.Length; if (byteCount == 0) { stream.Close(); return; } When my application executes, I get the following error message: "System.IO.IOExceptionn in MSCORLIB.dll. The file test.ipc is already in use" Can you please explain. Thanks, Muralli. David Browne wrote: Show quote > <muralidhar***@hotmail.com> wrote in message > news:1154775802.522701.32070@p79g2000cwp.googlegroups.com... > > Hi All, > > > > Can anyone explain the differences between FileShare.None and > > FileShare.ReadWrite in filestreams using C#.NET windows application? > > > > FileShare.None declares that you are unwilling to share access to the file > with anyone else. You will open the file exclusively and if any other > process attempts to open the file, an error will be raised. > FileShare.ReadWrite declares that you are willing to share both read an > write access to the file with other users. > > David GVN wrote:
Show quote > Thanks Mr.David. What happens is byteCount is not 0? You still have the file open, unless > > I have a C# windows legacy framework. In my framework, Iam able to > access a file using FileShare.None from two different applications. > But the same fails when I try to access from same application multiple > times. > The code that I had written is: > > FileStream stream = new FileStream(_MessageFile, > FileMode.Open, FileAccess.ReadWrite, FileShare.None); > int byteCount = (int)stream.Length; > if (byteCount == 0) > { > stream.Close(); > return; > } there's code that you didn't show that closes it. > That error indicates that you haven't closed the file after opening it the > When my application executes, I get the following error message: > > "System.IO.IOExceptionn in MSCORLIB.dll. The file test.ipc is > already in use" > Can you please explain. first time. Make sure that you call FileStream.Close (or Dispose) when you're done with the file. FileShare.None means that you can't even share access to the file with yourself, so you can have only one open handle to the file at a time. -cd Thanks Mr. Daniel.
I have added some more code to my application. The following is the new code: if (byteCount == 0) { stream.Close(); return; } byte[] bytes = new byte[byteCount]; stream.Lock(0, byteCount); stream.Read(bytes, 0, byteCount); stream.SetLength(0); stream.Unlock(0, byteCount); stream.Close(); Eventhough Iam getting the same exception. Can you please explain me what is the cause? Thanks, GVN. Carl Daniel [VC++ MVP] wrote: Show quote > GVN wrote: > > Thanks Mr.David. > > > > I have a C# windows legacy framework. In my framework, Iam able to > > access a file using FileShare.None from two different applications. > > But the same fails when I try to access from same application multiple > > times. > > The code that I had written is: > > > > FileStream stream = new FileStream(_MessageFile, > > FileMode.Open, FileAccess.ReadWrite, FileShare.None); > > int byteCount = (int)stream.Length; > > if (byteCount == 0) > > { > > stream.Close(); > > return; > > } > > What happens is byteCount is not 0? You still have the file open, unless > there's code that you didn't show that closes it. > > > > > When my application executes, I get the following error message: > > > > "System.IO.IOExceptionn in MSCORLIB.dll. The file test.ipc is > > already in use" > > Can you please explain. > > That error indicates that you haven't closed the file after opening it the > first time. Make sure that you call FileStream.Close (or Dispose) when > you're done with the file. FileShare.None means that you can't even share > access to the file with yourself, so you can have only one open handle to > the file at a time. > > -cd Hi All,
Can anyone help me in solving my problem? Thanks, GVN. GVN wrote: Show quote > Thanks Mr. Daniel. > > I have added some more code to my application. > > The following is the new code: > > if (byteCount == 0) > { > stream.Close(); > return; > } > byte[] bytes = new byte[byteCount]; > stream.Lock(0, byteCount); > stream.Read(bytes, 0, byteCount); > stream.SetLength(0); > stream.Unlock(0, byteCount); > stream.Close(); > > Eventhough Iam getting the same exception. Can you please explain me > what is the cause? > > Thanks, > > GVN. > > Carl Daniel [VC++ MVP] wrote: > > > GVN wrote: > > > Thanks Mr.David. > > > > > > I have a C# windows legacy framework. In my framework, Iam able to > > > access a file using FileShare.None from two different applications. > > > But the same fails when I try to access from same application multiple > > > times. > > > The code that I had written is: > > > > > > FileStream stream = new FileStream(_MessageFile, > > > FileMode.Open, FileAccess.ReadWrite, FileShare.None); > > > int byteCount = (int)stream.Length; > > > if (byteCount == 0) > > > { > > > stream.Close(); > > > return; > > > } > > > > What happens is byteCount is not 0? You still have the file open, unless > > there's code that you didn't show that closes it. > > > > > > > > When my application executes, I get the following error message: > > > > > > "System.IO.IOExceptionn in MSCORLIB.dll. The file test.ipc is > > > already in use" > > > Can you please explain. > > > > That error indicates that you haven't closed the file after opening it the > > first time. Make sure that you call FileStream.Close (or Dispose) when > > you're done with the file. FileShare.None means that you can't even share > > access to the file with yourself, so you can have only one open handle to > > the file at a time. > > > > -cd "GVN" <muralidhar***@hotmail.com> wrote in message Not from the information you have provided. If you post a simple, complete news:1155363939.506112.184200@p79g2000cwp.googlegroups.com... > Hi All, > Can anyone help me in solving my problem? > repro of your problem, then perhaps. David <muralidhar***@hotmail.com> wrote:
> Can anyone explain the differences between FileShare.None and Well, which part of the documentation didn't you understand? It looks > FileShare.ReadWrite in filestreams using C#.NET windows application? pretty clear to me - None stops the file from being shared with other access; ReadWrite allows it for both reading and writing. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
|||||||||||||||||||||||