C# - FileStream.CanRead Property

In this tutorial, we will learn about the FileStream.CanRead property with its usage, syntax, and example in C#.
Submitted by IncludeHelp, on November 17, 2017 [Last updated : March 27, 2023]

C# FileStream.CanRead Property

The CanRead is a property of FileStream class. It returns Boolean value true/false, that we can read file or not.

Syntax

bool CanRead

Parameter(s)

  • None

C# Example of FileStream.CanRead Property

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      byte[] b2 = new byte[6];

      FileStream f1;
      FileStream f2;

      f1 = new FileStream("abc.txt", FileMode.Open, FileAccess.Read);
      f2 = new FileStream("xyz.txt", FileMode.Open, FileAccess.Write);

      if (f1.CanRead)
        Console.WriteLine("Yes, It can read.");
      else
        Console.WriteLine("Yes, It can not read.");

      f1.Close();

      if (f2.CanRead)
        Console.WriteLine("Yes, It can read.");
      else
        Console.WriteLine("Yes, It can not read.");

      f2.Close();
    }
  }
}

Output

Yes, It can read.
Yes, It can not read.

Explanation

In above program, we need to remember, when we use "FileStream" class then we need to include System.IO namespace in the program.

C# FileStream Class Program »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.