Home » C# programs » C# FileStream class programs

C# program to demonstrate the use of CanSeek property

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

Given a file and we have to check whether file is able to seek or not using C# program.

FileStream.CanSeek

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

Syntax:

bool CanSeek

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


            f1.Close();

            if (f2.CanSeek)
                Console.WriteLine("Yes, It can be seeked.");
            else
                Console.WriteLine("Yes, It can not seeked.");


            f2.Close();
        }
    }
}

Output

Yes, It can be seeked.
Yes, It can be seeked.

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.