Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileInfo.Open Method (FileMode, FileAccess, FileShare)

Opens a file in the specified mode with read, write, or read/write access and the specified sharing option.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public FileStream Open (
	FileMode mode,
	FileAccess access,
	FileShare share
)
public FileStream Open (
	FileMode mode, 
	FileAccess access, 
	FileShare share
)
public function Open (
	mode : FileMode, 
	access : FileAccess, 
	share : FileShare
) : FileStream

Parameters

mode

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

access

A FileAccess constant specifying whether to open the file with Read, Write, or ReadWrite file access.

share

A FileShare constant specifying the type of access other FileStream objects have to this file.

Return Value

A FileStream object opened with the specified mode, access, and sharing options.

Exception typeCondition

SecurityException

The caller does not have the required permission.

ArgumentException

path is empty or contains only white spaces.

FileNotFoundException

The file is not found.

ArgumentNullException

One or more arguments is a null reference (Nothing in Visual Basic).

UnauthorizedAccessException

path is read-only or is a directory.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

IOException

The file is already open.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file.

File.Move

FileInfo.MoveTo

Copy a file.

File.Copy

FileInfo.CopyTo

Get the size of a file.

FileInfo.Length

Get the attributes of a file.

File.GetAttributes

Set the attributes of a file.

File.SetAttributes

Determine if a file exists.

File.Exists

Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Create a directory.

CreateDirectory

Directory

The following example demonstrates opening a file for reading and writing, but disallowing access to other users or processes.

using System;
using System.IO;

public class OpenTest 
{
    public static void Main() 
    {
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Open the file just specified such that no one else can use it.
        FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None );

        // Create another reference to the same file.
        FileInfo nextfi = new FileInfo("temp.txt");        

        try 
        {
            // Try opening the same file, which was locked by the previous process.
            nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read );

            Console.WriteLine("The file was not locked, and was opened by a second process.");
        } 
        catch (IOException) 
        {
            Console.WriteLine("The file could not be opened because it was locked by another process.");
        } 
        catch (Exception e) 
        {
            Console.WriteLine(e.ToString());
        }

        // Close the file so it can be deleted.
        fs.Close();
    }
}

import System.*;
import System.IO.*;

public class OpenTest
{
    public static void main(String[] args)
    {
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Open the file just specified such that no one else can use it.
        FileStream fs = fi.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite,
            FileShare.None);

        // Create another reference to the same file.
        FileInfo nextfi = new FileInfo("temp.txt");
        try {
            // Try opening the same file, which was locked by the
            // previous process.
            nextfi.Open(FileMode.OpenOrCreate, FileAccess.Read);
            Console.WriteLine("The file was not locked, and was opened by a " 
                + " second process.");
        }
        catch (IOException exp) {
            Console.WriteLine("The file could not be opened because it was " 
                + " locked by another process.");
        }
        catch (System.Exception e) {
            Console.WriteLine(e.ToString());
        }

        // Close the file so it can be deleted.
        fs.Close();
    } //main
} //OpenTest

import System;
import System.IO;

public class OpenTest {
    public static function Main() : void {

        // Open an existing file, or create a new one.
        var fi : FileInfo = new FileInfo("temp.txt");

        // Open the file just specified such that no one else can use it.
        var fs : FileStream = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None );

        // Create another reference to the same file.
        var nextfi : FileInfo = new FileInfo("temp.txt");        

        try {
            // Try opening the same file, which was locked by the previous process.
            nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read );

            Console.WriteLine("The file was not locked, and was opened by a second process.");
        } catch (e : IOException) {
            Console.WriteLine("The file could not be opened because it was locked by another process.");
        } catch (e : Exception) {
            Console.WriteLine(e.ToString());
        }

        // Close the file so it can be deleted.
        fs.Close();
    }
}
OpenTest.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft