Windows apps
Collapse the table of content
Expand the table of content
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
Not applicable.

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.

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.

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
//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.

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

//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 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2017 Microsoft