This documentation is archived and is not being maintained.

File.Open Method

Opens a FileStream on the specified path.

Overload List

Opens a FileStream on the specified path with read/write access.

Supported by the .NET Compact Framework.

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

Opens a FileStream on the specified path, with the specified mode and access.

Supported by the .NET Compact Framework.

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

Opens a FileStream on the specified path, having 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 Shared Function Open(String, FileMode, FileAccess, FileShare) As FileStream
[C#] public static FileStream Open(string, FileMode, FileAccess, FileShare);
[C++] public: static FileStream* Open(String*, FileMode, FileAccess, FileShare);
[JScript] public static function Open(String, FileMode, FileAccess, FileShare) : FileStream;

Example

[Visual Basic, C#, C++] The following example opens a file with read-only access and with file sharing disallowed.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Open. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim fs As FileStream

        ' Delete the file if it exists.
        If File.Exists(path) = False Then
            ' Create the file.
            fs = File.Create(path)
            Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

            ' Add some information to the file.
            fs.Write(info, 0, info.Length)
            fs.Close()
        End If

        ' Open the stream and read it back.
        fs = File.Open(path, FileMode.Open, FileAccess.Read)
        Dim b(1024) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop

        Try
            ' Try to get another handle to the same file.
            Dim fs2 As FileStream = File.Open(path, FileMode.Open)
            ' Do some task here.
            fs2.Close()
        Catch e As Exception
            Console.Write("Opening the file twice is disallowed.")
            Console.WriteLine(", as expected: {0}", e.ToString())
        End Try

        fs.Close()
    End Sub
End Class

[C#] 
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        if (!File.Exists(path)) 
        {
            // Create the file.
            using (FileStream fs = File.Create(path)) 
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");

                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }
        }

        // Open the stream and read it back.
        using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)) 
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0) 
            {
                Console.WriteLine(temp.GetString(b));
            }

            try 
            {
                // Try to get another handle to the same file.
                using (FileStream fs2 = File.Open(path, FileMode.Open)) 
                {
                    // Do some task here.
                }
            } 
            catch (Exception e) 
            {
                Console.Write("Opening the file twice is disallowed.");
                Console.WriteLine(", as expected: {0}", e.ToString());
            }
        }
    }
}

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

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    // Delete the file if it exists.
    if (!File::Exists(path)) {
        // Create the file.
        FileStream* fs = File::Create(path);
        try {
            Byte info[] = (new UTF8Encoding(true))->GetBytes(S"This is some text in the file.");

            // Add some information to the file.
            fs->Write(info, 0, info->Length);
        } __finally {
            if (fs) __try_cast<IDisposable*>(fs)->Dispose();
        }
    }

    // Open the stream and read it back.
    FileStream* fs = File::Open(path, FileMode::Open, FileAccess::Read, FileShare::None);
    try {
        Byte b[] = new Byte[1024];
        UTF8Encoding* temp = new UTF8Encoding(true);

        while (fs->Read(b,0,b->Length) > 0) {
            Console::WriteLine(temp->GetString(b));
        }

        try {
            // Try to get another handle to the same file.
            FileStream* fs2 = File::Open(path, FileMode::Open);
            try {
                // Do some task here.
            } __finally {
                if (fs2) __try_cast<IDisposable*>(fs2)->Dispose();
            }
        } catch (Exception* e) {
            Console::Write(S"Opening the file twice is disallowed.");
            Console::WriteLine(S", as expected: {0}", e);
        }
    } __finally {
        if (fs) __try_cast<IDisposable*>(fs)->Dispose();
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

File Class | File Members | System.IO Namespace

Show: