C# FileInfo Open(FileMode)

Description

FileInfo Open(FileMode) Opens a file in the specified mode.

Syntax

FileInfo.Open(FileMode) has the following syntax.


public FileStream Open(
  FileMode mode
)

Parameters

FileInfo.Open(FileMode) has the following parameters.

  • mode - A FileMode constant specifying the mode (for example, Open or Append) in which to open the file.

Returns

FileInfo.Open(FileMode) method returns A file opened in the specified mode, with read/write access and unshared.

Example

The following example opens a file, adds some information to the file, and reads the file.


//w  w w.j a  v  a2s .  com
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.Open(FileMode.Open)) 
        {
            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