次の方法で共有


File.Open メソッド

指定したパスの FileStream を開きます。

オーバーロードの一覧

指定したパスの FileStream を読み取り/書き込みアクセスで開きます。

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

指定したモードとアクセスで、指定したパスの FileStream を開きます。

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

読み取り、書き込み、または読み取り/書き込みアクセスを持つ指定モードと指定した共有オプションで、指定したパスの FileStream を開きます。

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

使用例

[Visual Basic, C#, C++] ファイル共有を禁止して、ファイルを読み取り専用アクセスで開く例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、Open のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

File クラス | File メンバ | System.IO 名前空間