Example

using System;
using System.IO;

namespace Samples
{
  public class FileModeSample
  {
    public static void Main()
    {
      string s = @"FileMode.txt";
      FileMode[] filemodes = {FileMode.Append,
                              FileMode.Create,
                              FileMode.CreateNew,
                              FileMode.Open,
                              FileMode.OpenOrCreate,
                              FileMode.Truncate};
      foreach(FileMode fm in filemodes)
      {
        OpenFile(s, fm,FileAccess.Write);
      }
    }
    private static void OpenFile(string s,
                                 FileMode fm,
                                 FileAccess a)
    {
      try
      {
        using(FileStream fs = File.Open(s, fm, a))
        {
          Console.WriteLine(
             "Can seek in {0} when opened with {1} and {2}: {3}",
                s, a, fm, fs.CanSeek);
        }
      }
      catch(Exception e)
      {
        Console.WriteLine(
           "Cannot open {0} with {1}, reason {2}",
           s, fm, e.Message);
      }
    }
  }
}
The output is
 Can seek in FileMode.txt when opened with Write and Append: ...

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.