FileShare parameter in filestream creation

  • Thread starter Thread starter muralidhargvn
  • Start date Start date
M

muralidhargvn

Hi All,

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

Thanks,

Murali.
 
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
 
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.
 
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.
 
GVN said:
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
 
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.
 
Hi All,
Can anyone help me in solving my problem?

Thanks,
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.
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.


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 said:
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
 
Back
Top