Home All Groups Group Topic Archive Search About

FileShare parameter in filestream creation

Author
5 Aug 2006 11:03 AM
muralidhargvn
Hi All,

   Can anyone explain the differences between FileShare.None and
FileShare.ReadWrite  in filestreams using C#.NET windows application?

Thanks,

Murali.

Author
5 Aug 2006 2:26 PM
David Browne
<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
Author
6 Aug 2006 6:07 AM
GVN
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
Author
6 Aug 2006 2:27 PM
Carl Daniel [VC++ MVP]
GVN wrote:
Show quote
> 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
Author
7 Aug 2006 5:13 AM
GVN
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
Author
12 Aug 2006 6:25 AM
GVN
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
Author
12 Aug 2006 3:42 PM
David Browne
"GVN" <muralidhar***@hotmail.com> wrote in message
news:1155363939.506112.184200@p79g2000cwp.googlegroups.com...
> Hi All,
>  Can anyone help me in solving my problem?
>

Not from the information you have provided.  If you post a simple, complete
repro of your problem, then perhaps.

David
Author
5 Aug 2006 2:56 PM
Jon Skeet [C# MVP]
<muralidhar***@hotmail.com> wrote:
>    Can anyone explain the differences between FileShare.None and
> FileShare.ReadWrite  in filestreams using C#.NET windows application?

Well, which part of the documentation didn't you understand? It looks
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

AddThis Social Bookmark Button