0

Recently I've faced with problem the I cannot share the file access between processes/threads. Simple code:

 FileStream open = File.Open("path", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

Next calling if FileOpen with FileAccess.Read and sharing - None or Read - throws the exeption.

It must be assumed that this code allows reading for anyone the same as "native" WinAPI function CreateFile which works just perfect in this case.

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern IntPtr CreateFileW(
         [MarshalAs(UnmanagedType.LPWStr)] string filename,
         [MarshalAs(UnmanagedType.U4)] EFileAccess access,
         [MarshalAs(UnmanagedType.U4)] EFileShare share,
         IntPtr securityAttributes,
         [MarshalAs(UnmanagedType.U4)] ECreationDisposition creationDisposition,
         [MarshalAs(UnmanagedType.U4)] EFileAttributes flagsAndAttributes,
         IntPtr templateFile);
IntPtr ptr = CreateFileW(path,
                        EFileAccess.GenericRead,
                        EFileShare.None,
                        IntPtr.Zero,
                        ECreationDisposition.OpenExisting,
                        EFileAttributes.Normal,
                        IntPtr.Zero);

So, is there any tricks to share file access using just .NET?

One more moment! Actually we are able to share the file with next code:

FileStream open = File.Open("path", FileMode.OpenOrCreate, **FileAccess.Read**, FileShare.Read);

But in my case - I do need that the first thread will be able to write file!!

Baranovskiy Dmitry
  • 463
  • 2
  • 5
  • 23
  • If you want subsequent threads to be able to read from and write to the file, you should be using `FileShare.ReadWrite` for the 3rd argument to `File.Open()`. I'm assuming you know the inherit danger in this (e.g. reading a file that's being modified, etc.) Look at this for more information: http://msdn.microsoft.com/en-us/library/system.io.fileshare%28v=vs.110%29.aspx – itsme86 Sep 27 '14 at 18:14
  • Look at the example FileStream st = File.Open(@"D:\WorkFile.txt", FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite); try { FileStream str = File.Open(@"D:\WorkFile.txt", FileMode.Open, FileAccess.Read, FileShare.Read); } catch (Exception) { }. I can share it for writing and reading for BOTH at the same time but I still can't make it accesable for writing and reading for first thread, and accesable only for reading for second thread. Not for both! Thanks! – Baranovskiy Dmitry Sep 27 '14 at 18:37
  • Initial task was: We have a program that check some files to process. If file is free, it lockes it and throws to another thread for processing (the files I locked so it should be shared for read for another thread). In parallel another Task check if file is accessable for writing - if file is locked the task skips it! This is two different applications so I have no another way as to share the file reading. I hope I havent confused you:) – Baranovskiy Dmitry Sep 27 '14 at 18:42

0 Answers0