Export (0) Print
Expand All

FileInfo.Open Method

Opens a file with various read/write and sharing privileges.

Overload List

Opens a file in the specified mode.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Open(FileMode) As FileStream
[C#] public FileStream Open(FileMode);
[C++] public: FileStream* Open(FileMode);
[JScript] public function Open(FileMode) : FileStream;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Open(FileMode, FileAccess) As FileStream
[C#] public FileStream Open(FileMode, FileAccess);
[C++] public: FileStream* Open(FileMode, FileAccess);
[JScript] public function Open(FileMode, FileAccess) : FileStream;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Open(FileMode, FileAccess, FileShare) As FileStream
[C#] public FileStream Open(FileMode, FileAccess, FileShare);
[C++] public: FileStream* Open(FileMode, FileAccess, FileShare);
[JScript] public function Open(FileMode, FileAccess, FileShare) : FileStream;

Example

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

[Visual Basic] 
Imports System
Imports System.IO

Public Class OpenTest

    Public Shared Sub Main()
        ' Open an existing file, or create a new one.
        Dim fi As New FileInfo("temp.txt")

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

        ' Create another reference to the same file.
        Dim nextfi As 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 i as IOException
            Console.WriteLine(i.ToString())
        Catch e As Exception
            Console.WriteLine(e.ToString())
        End Try

        ' Close the file so it can be deleted.
        fs.Close()
    End Sub 'Main
End Class 'OpenTest

[C#] 
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();
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    // Open an existing file, or create a new one.
    FileInfo* fi = new FileInfo(S"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(S"temp.txt");

    try {
        // Try opening the same file, which was locked by the previous process.
        nextfi->Open(FileMode::OpenOrCreate, FileAccess::Read);
        Console::WriteLine(S"The file was not locked, and was opened by a second process.");
    } catch (IOException*) {
        Console::WriteLine(S"The file could not be opened because it was locked by another process.");
    } catch (Exception* e) {
        Console::WriteLine(e);
    }

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

[JScript] 
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();

See Also

FileInfo Class | FileInfo Members | System.IO Namespace

Show:
© 2014 Microsoft