Export (0) Print
Expand All

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
)

Parameters

mode
Type: System.IO.FileMode

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

access
Type: System.IO.FileAccess

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

share
Type: System.IO.FileShare

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

Return Value

Type: System.IO.FileStream
A FileStream object opened with the specified mode, access, and sharing options.

ExceptionCondition
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 null.

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.

For a list of common I/O tasks, see Common I/O Tasks.

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();
    }
}

//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
// 
//The file could not be opened because it was locked by another process.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft