C# FileInfo OpenRead

Description

FileInfo OpenRead Creates a read-only FileStream.

Syntax

FileInfo.OpenRead has the following syntax.


public FileStream OpenRead()

Returns

FileInfo.OpenRead method returns A new read-only FileStream object.

Example

The following example opens a file as read-only and reads from it.


//from   w ww . jav  a  2s  .  c o m
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        using (FileStream fs = fi.OpenRead()) 
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0) 
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter