Reputation: 3053
in my app I'm trying to read a stream from the file system. This is my method:
public Stream ReadStream (string path)
{
return new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
This methods sometimes works, but most of the times it thorw the following exception:
Sharing violation on path /data/data/MyPath/11.png
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
Can you help me? Thanks a lot!
Upvotes: 0
Views: 657
Reputation: 51390
FileShare.None
means you won't be able to open the stream if anything else on the system has a file handle open on the same file. It also means other processes won't be able to open the file as long as your handle is open.
The solution depends on your needs, but you could for instance change it to FileShare.Read
to prohibit other processes to write in the file at the same time as you, but still allow them to open it for reading.
And, looking at your function's name, I guess you could just replace it with File.OpenRead
.
EDIT: Just noticed your question is about mono, I'm not sure if what I said here holds true.
Upvotes: 1