Home » C# programs » C# FileStream class programs

C# program to demonstrate the use of CanRead property

In this article, we will learn about, can we check a file stream is able to read file or not? Here we will use CanRead property of FileStream class.
Submitted by IncludeHelp, on November 17, 2017

Given a file and we have to check whether file can be read or not using C# program.

FileStream.CanRead

This is a method of "FileStream" class, It returns Boolean value true/false, that we can read file or not.

Syntax:

bool CanRead

Example:

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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.