Example

using System;
using System.IO;

namespace Samples
{
  public class FileShareSample
  {
    public static void Main()
    {
      string s = @"open.txt";
      FileShare[] fileShares = { FileShare.None,
                          FileShare.Read,
                          FileShare.ReadWrite,
                          FileShare.Write};
      foreach(FileShare fs in fileShares)
      {
        using(FileStream a = File.Open(s,
                                       FileMode.OpenOrCreate,
                                       FileAccess.ReadWrite,
                                       fs))
        {
          Console.WriteLine("Opened with FileShare: {0}", fs);
          OpenAgain(s);
          Console.WriteLine();
        }
      }
    }
    public static void OpenAgain(string s)
    {
      try
      {
        using(FileStream a = File.Open(s,
                                       FileMode.Open,
                                       FileAccess.Read,
                                       FileShare.ReadWrite))
        {
          Console.WriteLine("Can open file again to read");
        }
      }
      catch(Exception)
      {
        Console.WriteLine("Can not open file again to read");
      }
    }
  }
}
The output is
 Opened ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.