Home » C# programs » C# FileStream class programs

C# program to demonstrate the use of CanWrite property

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

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

FileStream.CanWrite

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

Syntax:

bool CanWrite

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.CanWrite)
                Console.WriteLine("Yes, It can write.");
            else
                Console.WriteLine("Yes, It can not write.");


            f1.Close();

            if (f2.CanWrite)
                Console.WriteLine("Yes, It can write.");
            else
                Console.WriteLine("Yes, It can not write.");

            f2.Close();
        }
    }
}

Output

Yes, It can not write.
Yes, It can write.

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.