Check file mode - CSharp File IO

CSharp examples for File IO:File Command

Description

Check file mode

Demo Code

using System;//  w  w  w  . j  av a2s. c o  m
using static System.Console;
using System.IO;
class Program
{
   static void Main(string[] args)
   {
      string path = @"C:\a\";
      Stream s = File.Open(Path.Combine(path, "main.cs"),FileMode.OpenOrCreate);
      switch (s)
      {
         case FileStream writeableFile when s.CanWrite:
         WriteLine("The stream is to a file that I can write to.");
         break;
         case FileStream readOnlyFile:
         WriteLine("The stream is to a read-only file.");
         break;
         case MemoryStream ms:
         WriteLine("The stream is to a memory address.");
         break;
         default: // always evaluated last despite its current position
         WriteLine("The stream is some other type.");
         break;
         case null:
         WriteLine("The stream is null.");
         break;
      }
   }
}

Result


Related Tutorials