FileStream コンストラクター

定義

FileStream クラスの新しいインスタンスを初期化します。

オーバーロード

FileStream(SafeFileHandle, FileAccess)

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可を使用して、指定したファイル ハンドル用に初期化します。

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、アクセス権、共有アクセス許可、バッファー サイズ、追加のファイル オプション、アクセス制御、および監査セキュリティを使用して初期化します。

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、アクセス権、共有アクセス許可、バッファー サイズ、および追加のファイル オプションを使用して初期化します。

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、同一のファイルに対して他の FileStream が保有できるアクセス、バッファー サイズ、および追加のファイル オプションを使用して初期化します。

FileStream(String, FileMode, FileAccess, FileShare, Int32)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、およびバッファー サイズを使用して初期化します。

FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)
古い.
古い.
古い.

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、FileStream インスタンスの所有権、バッファー サイズ、および同期状態または非同期状態を使用して、指定したファイル ハンドル用に初期化します。

FileStream(String, FileMode, FileAccess, FileShare)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、および共有アクセス許可を使用して初期化します。

FileStream(IntPtr, FileAccess, Boolean, Int32)
古い.
古い.
古い.

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、FileStream インスタンスの所有権、およびバッファー サイズを使用して、指定したファイル ハンドル用に初期化します。

FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、バッファー サイズ、および同期状態または非同期状態を使用して初期化します。

FileStream(String, FileMode, FileAccess)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、および読み取り/書き込みアクセス許可を使用して初期化します。

FileStream(IntPtr, FileAccess, Boolean)
古い.
古い.
古い.

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可と FileStream インスタンスの所有権を使用して、指定したファイル ハンドル用に初期化します。

FileStream(SafeFileHandle, FileAccess, Int32)

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、およびバッファー サイズを使用して、指定したファイル ハンドル用に初期化します。

FileStream(SafeFileHandle, FileAccess, Int32, Boolean)

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、バッファー サイズ、および同期状態または非同期状態を使用して、指定したファイル ハンドル用に初期化します。

FileStream(String, FileStreamOptions)

指定したパス、作成モード、読み取り/書き込みおよび共有アクセス許可、バッファー サイズ、追加のファイル オプション、事前割り当てサイズ、および他の FileStreams が同じファイルに対して持つアクセス権を使用して、 クラスの新しいインスタンス FileStream を初期化します。

FileStream(String, FileMode)

FileStream クラスの新しいインスタンスを、指定したパスと作成モードを使用して初期化します。

FileStream(IntPtr, FileAccess)
古い.
古い.
古い.

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可を使用して、指定したファイル ハンドル用に初期化します。

FileStream(SafeFileHandle, FileAccess)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可を使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(Microsoft::Win32::SafeHandles::SafeFileHandle ^ handle, System::IO::FileAccess access);
public FileStream (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.IO.FileAccess access);
new System.IO.FileStream : Microsoft.Win32.SafeHandles.SafeFileHandle * System.IO.FileAccess -> System.IO.FileStream
Public Sub New (handle As SafeFileHandle, access As FileAccess)

パラメーター

handle
SafeFileHandle

現在の FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead および CanWrite プロパティを設定する列挙値のビットごとの組み合わせ。

例外

accessFileAccess のフィールドではありません。

呼び出し元に、必要なアクセス許可がありません。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

が呼び出されると Close 、ハンドルも閉じられ、ファイルのハンドル数がデクリメントされます。

FileStream は、ハンドルを排他的に制御していることを前提としています。 ハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

FileShare.Read は、パラメーターのないコンストラクター FileStream の既定値です FileShare

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、アクセス権、共有アクセス許可、バッファー サイズ、追加のファイル オプション、アクセス制御、および監査セキュリティを使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::Security::AccessControl::FileSystemRights rights, System::IO::FileShare share, int bufferSize, System::IO::FileOptions options, System::Security::AccessControl::FileSecurity ^ fileSecurity);
public FileStream (string path, System.IO.FileMode mode, System.Security.AccessControl.FileSystemRights rights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity fileSecurity);
new System.IO.FileStream : string * System.IO.FileMode * System.Security.AccessControl.FileSystemRights * System.IO.FileShare * int * System.IO.FileOptions * System.Security.AccessControl.FileSecurity -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, rights As FileSystemRights, share As FileShare, bufferSize As Integer, options As FileOptions, fileSecurity As FileSecurity)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

rights
FileSystemRights

ファイルのアクセス規則および監査規則の作成時に使用するアクセス権を決定する列挙値のビットごとの組み合わせ。

share
FileShare

ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

options
FileOptions

その他のファイル オプションを指定する列挙値のビットごとの組み合わせ。

fileSecurity
FileSecurity

ファイルのアクセス制御と監査セキュリティを決定するオブジェクト。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

bufferSize が負の値またはゼロです。

または

modeaccess、または share に正しくない値が含まれています。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

または

options に対して Encrypted が指定されていますが、ファイルの暗号化は現在のプラットフォームではサポートされていません。

指定した path、ファイル名、またはその両方がシステム定義の最大長を超えています。

現在のオペレーティング システムは Windows NT 以降ではありません。

次の例では、ファイルにデータを書き込み、 オブジェクトを使用してデータを FileStream 読み取ります。

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::AccessControl;
using namespace System::Security::Principal;

int main()
{
    try
    {
        // Create a file and write data to it.

        // Create an array of bytes.
        array<Byte>^ messageByte =
            Encoding::ASCII->GetBytes("Here is some data.");

        // Specify an access control list (ACL)
        FileSecurity^ fs = gcnew FileSecurity();

        fs->AddAccessRule(
            gcnew FileSystemAccessRule("MYDOMAIN\\MyAccount",
            FileSystemRights::Modify, AccessControlType::Allow));

        // Create a file using the FileStream class.
        FileStream^ fWrite = gcnew FileStream("test.txt",
            FileMode::Create, FileSystemRights::Modify,
            FileShare::None, 8, FileOptions::None, fs);

        // Write the number of bytes to the file.
        fWrite->WriteByte((Byte)messageByte->Length);

        // Write the bytes to the file.
        fWrite->Write(messageByte, 0, messageByte->Length);

        // Close the stream.
        fWrite->Close();

        // Open a file and read the number of bytes.

        FileStream^ fRead = 
            gcnew FileStream("test.txt", FileMode::Open);

        // The first byte is the string length.
        int length = (int)fRead->ReadByte();

        // Create a new byte array for the data.
        array<Byte>^ readBytes = gcnew array<Byte>(length);

        // Read the data from the file.
        fRead->Read(readBytes, 0, readBytes->Length);

        // Close the stream.
        fRead->Close();

        // Display the data.
        Console::WriteLine(Encoding::ASCII->GetString(readBytes));

        Console::WriteLine("Done writing and reading data.");
    }

    catch (IdentityNotMappedException^)
    {
        Console::WriteLine("You need to use your own credentials " +
            " 'MYDOMAIN\\MyAccount'.");
    }

    catch (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}
using System;
using System.IO;
using System.Text;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileStreamExample
    {
        public static void Main()
        {
            try
            {
                // Create a file and write data to it.

                // Create an array of bytes.
                byte[] messageByte = Encoding.ASCII.GetBytes("Here is some data.");

                // Specify an access control list (ACL)
                FileSecurity fs = new FileSecurity();

                fs.AddAccessRule(new FileSystemAccessRule(@"DOMAINNAME\AccountName",
                                                            FileSystemRights.ReadData,
                                                            AccessControlType.Allow));

                // Create a file using the FileStream class.
                FileStream fWrite = new FileStream("test.txt", FileMode.Create, FileSystemRights.Modify, FileShare.None, 8, FileOptions.None, fs);

                // Write the number of bytes to the file.
                fWrite.WriteByte((byte)messageByte.Length);

                // Write the bytes to the file.
                fWrite.Write(messageByte, 0, messageByte.Length);

                // Close the stream.
                fWrite.Close();

                // Open a file and read the number of bytes.

                FileStream fRead = new FileStream("test.txt", FileMode.Open);

                // The first byte is the string length.
                int length = (int)fRead.ReadByte();

                // Create a new byte array for the data.
                byte[] readBytes = new byte[length];

                // Read the data from the file.
                fRead.Read(readBytes, 0, readBytes.Length);

                // Close the stream.
                fRead.Close();

                // Display the data.
                Console.WriteLine(Encoding.ASCII.GetString(readBytes));

                Console.WriteLine("Done writing and reading data.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }
    }
}
open System
open System.IO;
open System.Text
open System.Security.AccessControl;

try
    // Create a file and write data to it.

    // Create an array of bytes.
    let messageByte = Encoding.ASCII.GetBytes "Here is some data."
    // Specify an access control list (ACL)
    let fs = FileSecurity()

    FileSystemAccessRule(@"DOMAINNAME\AccountName", FileSystemRights.ReadData, AccessControlType.Allow)
    |> fs.AddAccessRule
    // Create a file using the FileStream class.
    let fWrite = new FileStream("test.txt", FileMode.Create, FileSystemRights.Modify, FileShare.None, 8, FileOptions.None, fs)

    // Write the number of bytes to the file.
    byte messageByte.Length
    |> fWrite.WriteByte

    // Write the bytes to the file.
    fWrite.Write(messageByte, 0, messageByte.Length);

    // Close the stream.
    fWrite.Close();

    // Open a file and read the number of bytes.

    let fRead = new FileStream("test.txt", FileMode.Open)

    // The first byte is the string length.
    let length = fRead.ReadByte() |> int

    // Create a new byte array for the data.
    let readBytes = Array.zeroCreate length

    // Read the data from the file.
    fRead.Read(readBytes, 0, readBytes.Length);

    // Close the stream.
    fRead.Close();

    // Display the data.
    printfn $"{Encoding.ASCII.GetString readBytes}"
    printfn "Done writing and reading data."

with e ->
    printfn $"{e}"
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl



Module FileStreamExample

    Sub Main()
        Try
            ' Create a file and write data to it.
            ' Create an array of bytes.
            Dim messageByte As Byte() = Encoding.ASCII.GetBytes("Here is some data.")

            ' Specify an access control list (ACL)
            Dim fs As New FileSecurity()

            fs.AddAccessRule(New FileSystemAccessRule("DOMAINNAME\AccountName", FileSystemRights.ReadData, AccessControlType.Allow))

            ' Create a file using the FileStream class.
            Dim fWrite As New FileStream("test.txt", FileMode.Create, FileSystemRights.Modify, FileShare.None, 8, FileOptions.None, fs)

            ' Write the number of bytes to the file.
            fWrite.WriteByte(System.Convert.ToByte(messageByte.Length))

            ' Write the bytes to the file.
            fWrite.Write(messageByte, 0, messageByte.Length)

            ' Close the stream.
            fWrite.Close()


            ' Open a file and read the number of bytes.
            Dim fRead As New FileStream("test.txt", FileMode.Open)

            ' The first byte is the string length.
            Dim length As Integer = Fix(fRead.ReadByte())

            ' Create a new byte array for the data.
            Dim readBytes(length) As Byte

            ' Read the data from the file.
            fRead.Read(readBytes, 0, readBytes.Length)

            ' Close the stream.
            fRead.Close()

            ' Display the data.
            Console.WriteLine(Encoding.ASCII.GetString(readBytes))

            Console.WriteLine("Done writing and reading data.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

        Console.ReadLine()

    End Sub
End Module

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

この FileStream コンストラクターを使用して、ファイルの作成時にアクセス権を適用します。 既存のファイルに対する権限にアクセスまたは変更するには、 メソッドと SetAccessControl メソッドの使用をGetAccessControl検討してください。

パラメーターは fileOptions 、オブジェクトの作成時に利用できるより高度な操作へのアクセスを FileStream 提供するために使用されます。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

重要

このコンストラクターは .NET Core に存在しません。 代わりに、.NET Core 3.1 以降では、アセンブリCreate(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)内の クラスのFileSystemAclExtensions拡張メソッド をSystem.Security.AccessControl使用できます。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、アクセス権、共有アクセス許可、バッファー サイズ、および追加のファイル オプションを使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::Security::AccessControl::FileSystemRights rights, System::IO::FileShare share, int bufferSize, System::IO::FileOptions options);
public FileStream (string path, System.IO.FileMode mode, System.Security.AccessControl.FileSystemRights rights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options);
new System.IO.FileStream : string * System.IO.FileMode * System.Security.AccessControl.FileSystemRights * System.IO.FileShare * int * System.IO.FileOptions -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, rights As FileSystemRights, share As FileShare, bufferSize As Integer, options As FileOptions)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

rights
FileSystemRights

ファイルのアクセス規則および監査規則の作成時に使用するアクセス権を決定する列挙値のビットごとの組み合わせ。

share
FileShare

ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

options
FileOptions

その他のファイル オプションを指定する列挙値のビットごとの組み合わせ。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

bufferSize が負の値またはゼロです。

または

modeaccess、または share に正しくない値が含まれています。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

現在のオペレーティング システムは Windows NT 以降ではありません。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

または

options に対して Encrypted が指定されていますが、ファイルの暗号化は現在のプラットフォームではサポートされていません。

指定した path、ファイル名、またはその両方がシステム定義の最大長を超えています。

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

この FileStream コンストラクターを使用して、ファイルの作成時にアクセス権を適用します。 既存のファイルに対する権限にアクセスまたは変更するには、 メソッドと SetAccessControl メソッドの使用をGetAccessControl検討してください。

パラメーターは fileOptions 、オブジェクトの作成時に利用できるより高度な操作へのアクセスを FileStream 提供するために使用されます。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、同一のファイルに対して他の FileStream が保有できるアクセス、バッファー サイズ、および追加のファイル オプションを使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access, System::IO::FileShare share, int bufferSize, System::IO::FileOptions options);
public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options);
new System.IO.FileStream : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare * int * System.IO.FileOptions -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, access As FileAccess, share As FileShare, bufferSize As Integer, options As FileOptions)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

access
FileAccess

FileStream オブジェクトがファイルにアクセスする方法を決定する列挙値のビットごとの組み合わせ。 これにより、FileStream オブジェクトの CanRead および CanWrite プロパティによって返される値も決まります。 path がディスク ファイルを指定している場合、CanSeektrue になります。

share
FileShare

ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

options
FileOptions

その他のファイル オプションを指定する列挙値のビットごとの組み合わせ。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

bufferSize が負の値またはゼロです。

または

modeaccess、または share に正しくない値が含まれています。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

または

options に対して Encrypted が指定されていますが、ファイルの暗号化は現在のプラットフォームではサポートされていません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

次の例では、ファイルにデータを書き込み、 オブジェクトを使用してデータを FileStream 読み取ります。

#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::AccessControl;

int main()
{
    try
    {
        // Create a file and write data to it.

        // Create an array of bytes.
        array<Byte>^ messageByte =
            Encoding::ASCII->GetBytes("Here is some data.");

        // Create a file using the FileStream class.
        FileStream^ fWrite = gcnew FileStream("test.txt", FileMode::Create,
            FileAccess::ReadWrite, FileShare::None, 8, FileOptions::None);

        // Write the number of bytes to the file.
        fWrite->WriteByte((Byte)messageByte->Length);

        // Write the bytes to the file.
        fWrite->Write(messageByte, 0, messageByte->Length);

        // Close the stream.
        fWrite->Close();


        // Open a file and read the number of bytes.

        FileStream^ fRead = gcnew FileStream("test.txt", 
            FileMode::Open);

        // The first byte is the string length.
        int length = (int)fRead->ReadByte();

        // Create a new byte array for the data.
        array<Byte>^ readBytes = gcnew array<Byte>(length);

        // Read the data from the file.
        fRead->Read(readBytes, 0, readBytes->Length);

        // Close the stream.
        fRead->Close();

        // Display the data.
        Console::WriteLine(Encoding::ASCII->GetString(readBytes));

        Console::WriteLine("Done writing and reading data.");
    }
    catch (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}
using System;
using System.IO;
using System.Text;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileStreamExample
    {
        public static void Main()
        {
            try
            {
                // Create a file and write data to it.

                // Create an array of bytes.
                byte[] messageByte = Encoding.ASCII.GetBytes("Here is some data.");

                // Create a file using the FileStream class.
                FileStream fWrite = new FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None);

                // Write the number of bytes to the file.
                fWrite.WriteByte((byte)messageByte.Length);

                // Write the bytes to the file.
                fWrite.Write(messageByte, 0, messageByte.Length);

                // Close the stream.
                fWrite.Close();

                // Open a file and read the number of bytes.

                FileStream fRead = new FileStream("test.txt", FileMode.Open);

                // The first byte is the string length.
                int length = (int)fRead.ReadByte();

                // Create a new byte array for the data.
                byte[] readBytes = new byte[length];

                // Read the data from the file.
                fRead.Read(readBytes, 0, readBytes.Length);

                // Close the stream.
                fRead.Close();

                // Display the data.
                Console.WriteLine(Encoding.ASCII.GetString(readBytes));

                Console.WriteLine("Done writing and reading data.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }
    }
}
open System.IO
open System.Text

try
    // Create a file and write data to it.

    // Create an array of bytes.
    let messageByte = Encoding.ASCII.GetBytes "Here is some data."

    // Create a file using the FileStream class.
    let fWrite =
        new FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None)

    // Write the number of bytes to the file.
    byte messageByte.Length |> fWrite.WriteByte

    // Write the bytes to the file.
    fWrite.Write(messageByte, 0, messageByte.Length)

    // Close the stream.
    fWrite.Close()

    // Open a file and read the number of bytes.

    let fRead = new FileStream("test.txt", FileMode.Open)

    // The first byte is the string length.
    let length = fRead.ReadByte() |> int

    // Create a new byte array for the data.
    let readBytes = Array.zeroCreate length

    // Read the data from the file.
    fRead.Read(readBytes, 0, readBytes.Length) |> ignore

    // Close the stream.
    fRead.Close()

    // Display the data.
    printfn $"{Encoding.ASCII.GetString readBytes}"

    printfn "Done writing and reading data."

with e ->
    printfn $"{e}"
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl



Module FileStreamExample

    Sub Main()
        Try
            ' Create a file and write data to it.
            ' Create an array of bytes.
            Dim messageByte As Byte() = Encoding.ASCII.GetBytes("Here is some data.")

            ' Create a file using the FileStream class.
            Dim fWrite As New FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None)

            ' Write the number of bytes to the file.
            fWrite.WriteByte(System.Convert.ToByte(messageByte.Length))

            ' Write the bytes to the file.
            fWrite.Write(messageByte, 0, messageByte.Length)

            ' Close the stream.
            fWrite.Close()


            ' Open a file and read the number of bytes.
            Dim fRead As New FileStream("test.txt", FileMode.Open)

            ' The first byte is the string length.
            Dim length As Integer = Fix(fRead.ReadByte())

            ' Create a new byte array for the data.
            Dim readBytes(length) As Byte

            ' Read the data from the file.
            fRead.Read(readBytes, 0, readBytes.Length)

            ' Close the stream.
            fRead.Close()

            ' Display the data.
            Console.WriteLine(Encoding.ASCII.GetString(readBytes))

            Console.WriteLine("Done writing and reading data.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

        Console.ReadLine()

    End Sub
End Module

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

パラメーターは fileOptions 、オブジェクトの作成時に利用できるより高度な操作へのアクセスを FileStream 提供するために使用されます。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileAccess, FileShare, Int32)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、およびバッファー サイズを使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access, System::IO::FileShare share, int bufferSize);
public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize);
new System.IO.FileStream : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare * int -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, access As FileAccess, share As FileShare, bufferSize As Integer)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

access
FileAccess

FileStream オブジェクトがファイルにアクセスする方法を決定する列挙値のビットごとの組み合わせ。 これにより、FileStream オブジェクトの CanRead および CanWrite プロパティによって返される値も決まります。 path がディスク ファイルを指定している場合、CanSeektrue になります。

share
FileShare

ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

bufferSize が負の値またはゼロです。

または

modeaccess、または share に正しくない値が含まれています。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202

注意事項

This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、FileStream インスタンスの所有権、バッファー サイズ、および同期状態または非同期状態を使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(IntPtr handle, System::IO::FileAccess access, bool ownsHandle, int bufferSize, bool isAsync);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  https://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize, bool isAsync);
[System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize, bool isAsync);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize, bool isAsync);
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize, bool isAsync);
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  https://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int * bool -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int * bool -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int * bool -> System.IO.FileStream
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int * bool -> System.IO.FileStream
Public Sub New (handle As IntPtr, access As FileAccess, ownsHandle As Boolean, bufferSize As Integer, isAsync As Boolean)

パラメーター

handle
IntPtr

nativeint

この FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead および CanWrite プロパティを設定する列挙値のビットごとの組み合わせ。

ownsHandle
Boolean

この FileStream インスタンスがファイル ハンドルを所有する場合は true。それ以外の場合は false

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

isAsync
Boolean

ハンドルが非同期的に開かれた場合 (重複 I/O モード用) は true。それ以外の場合は false

属性

例外

accessFileAccess.Read 未満であるか、FileAccess.ReadWrite 以上であるか、または bufferSize が 0 以下です。

ハンドルが正しくありません。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

オブジェクトには FileStream 、指定されたファイルへのアクセス権が付与されます。 ハンドルの所有権は、指定したとおりになります。 これが FileStream ハンドルを所有している場合は、 メソッドの Close 呼び出しでもハンドルが閉じられます。 特に、ファイルのハンドル数はデクリメントされます。 オブジェクトには FileStream 、指定されたバッファー サイズが指定されています。

FileStream は、ハンドルを排他的に制御していることを前提としています。 ハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。 または、この FileStream コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。

FileShare.Read は、パラメーターのないコンストラクター FileStream の既定値です FileShare

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileAccess, FileShare)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、および共有アクセス許可を使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access, System::IO::FileShare share);
public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share);
new System.IO.FileStream : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, access As FileAccess, share As FileShare)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

access
FileAccess

FileStream オブジェクトがファイルにアクセスする方法を決定する列挙値のビットごとの組み合わせ。 これにより、FileStream オブジェクトの CanRead および CanWrite プロパティによって返される値も決まります。 path がディスク ファイルを指定している場合、CanSeektrue になります。

share
FileShare

ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

mode に無効な値が含まれています。

このコード例は、 メソッドに対して提供されるより大きな例の Lock 一部です。

FileStream^ fileStream = gcnew FileStream( "Test#@@#.dat",FileMode::OpenOrCreate,FileAccess::ReadWrite,FileShare::ReadWrite );
using(FileStream fileStream = new FileStream(
    "Test#@@#.dat", FileMode.OpenOrCreate,
    FileAccess.ReadWrite, FileShare.ReadWrite))
use fileStream =
    new FileStream("Test#@@#.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim aFileStream As New FileStream( _
    "Test#@@#.dat", FileMode.OpenOrCreate, _
    FileAccess.ReadWrite, FileShare.ReadWrite)

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

コンストラクターには、ファイルへの読み取り/書き込みアクセス権が与えられ、読み取りアクセス権を共有して開かれます (つまり、このまたは別のプロセスによる書き込みのためにファイルを開く要求は、オブジェクトが閉じられるまで FileStream 失敗しますが、読み取り試行は成功します)。 バッファー サイズは、既定のサイズである 4096 バイト (4 KB) に設定されます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(IntPtr, FileAccess, Boolean, Int32)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202

注意事項

This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、FileStream インスタンスの所有権、およびバッファー サイズを使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(IntPtr handle, System::IO::FileAccess access, bool ownsHandle, int bufferSize);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  https://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize);
[System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize);
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle, int bufferSize);
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  https://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int -> System.IO.FileStream
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool * int -> System.IO.FileStream
Public Sub New (handle As IntPtr, access As FileAccess, ownsHandle As Boolean, bufferSize As Integer)

パラメーター

handle
IntPtr

nativeint

この FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead および CanWrite プロパティを設定する列挙値のビットごとの組み合わせ。

ownsHandle
Boolean

この FileStream インスタンスがファイル ハンドルを所有する場合は true。それ以外の場合は false

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

属性

例外

bufferSize が負の値です。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

オブジェクトには FileStream 、指定されたファイルへのアクセス権が付与されます。 ハンドルの所有権は、指定したとおりになります。 これが FileStream ハンドルを所有している場合は、 メソッドの Close 呼び出しでもハンドルが閉じられます。 特に、ファイルのハンドル数はデクリメントされます。 オブジェクトには FileStream 、指定されたバッファー サイズが指定されています。

FileStream は、ハンドルを排他的に制御していることを前提としています。 ハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。 または、この FileStream コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。

FileShare.Read は、パラメーターのないコンストラクター FileStream の既定値です FileShare

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、バッファー サイズ、および同期状態または非同期状態を使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access, System::IO::FileShare share, int bufferSize, bool useAsync);
public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, int bufferSize, bool useAsync);
new System.IO.FileStream : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare * int * bool -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, access As FileAccess, share As FileShare, bufferSize As Integer, useAsync As Boolean)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

access
FileAccess

FileStream オブジェクトがファイルにアクセスする方法を決定する列挙値のビットごとの組み合わせ。 これにより、FileStream オブジェクトの CanRead および CanWrite プロパティによって返される値も決まります。 path がディスク ファイルを指定している場合、CanSeektrue になります。

share
FileShare

ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

useAsync
Boolean

非同期 I/O または同期 I/O のどちらを使用するかを指定します。 ただし、基になるオペレーティング システムが非同期 I/O をサポートしていないことがあります。したがって、true を指定しても、プラットフォームによってはハンドルが同期的に開かれることがあります。 非同期的に開いた場合、BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) メソッドと BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) メソッドは、大量の読み取りまたは書き込み時にはパフォーマンスがより高くなりますが、少量の読み取りまたは書き込み時にはパフォーマンスが非常に低くなることがあります。 アプリケーションが非同期 I/O を利用するように設計されている場合は、useAsync パラメーターを true に設定します。 非同期 I/O を正しく使用すると、アプリケーションが 10 倍ほど高速化することがあります。ただし、非同期 I/O 用にアプリケーションを再設計せずに非同期 I/O を使用すると、パフォーマンスが 10 分の 1 ほど低下することがあります。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

bufferSize が負の値またはゼロです。

または

modeaccess、または share に正しくない値が含まれています。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

次のコード例は、非同期的にファイルにデータを書き込み、データが正しく書き込まれたことを確認する方法を示しています。 Stateメイン スレッドから メソッドおよび EndWriteCallback メソッドに情報を渡すために、 オブジェクトがEndReadCallback作成されます。

using namespace System;
using namespace System::IO;
using namespace System::Threading;

// Maintain state information to be passed to 
// EndWriteCallback and EndReadCallback.
ref class State
{
private:

   // fStream is used to read and write to the file.
   FileStream^ fStream;

   // writeArray stores data that is written to the file.
   array<Byte>^writeArray;

   // readArray stores data that is read from the file.
   array<Byte>^readArray;

   // manualEvent signals the main thread 
   // when verification is complete.
   ManualResetEvent^ manualEvent;

public:
   State( FileStream^ fStream, array<Byte>^writeArray, ManualResetEvent^ manualEvent )
   {
      this->fStream = fStream;
      this->writeArray = writeArray;
      this->manualEvent = manualEvent;
      readArray = gcnew array<Byte>(writeArray->Length);
   }


   property FileStream^ FStream 
   {
      FileStream^ get()
      {
         return fStream;
      }

   }

   property array<Byte>^ WriteArray 
   {
      array<Byte>^ get()
      {
         return writeArray;
      }

   }

   property array<Byte>^ ReadArray 
   {
      array<Byte>^ get()
      {
         return readArray;
      }

   }

   property ManualResetEvent^ ManualEvent 
   {
      ManualResetEvent^ get()
      {
         return manualEvent;
      }

   }

};

ref class FStream
{
private:

   // When BeginRead is finished reading data from the file, the 
   // EndReadCallback method is called to end the asynchronous 
   // read operation and then verify the data.
   static void EndReadCallback( IAsyncResult^ asyncResult )
   {
      State^ tempState = dynamic_cast<State^>(asyncResult->AsyncState);
      int readCount = tempState->FStream->EndRead( asyncResult );
      int i = 0;
      while ( i < readCount )
      {
         if ( tempState->ReadArray[ i ] != tempState->WriteArray[ i++ ] )
         {
            Console::WriteLine( "Error writing data." );
            tempState->FStream->Close();
            return;
         }
      }

      Console::WriteLine( "The data was written to {0} "
      "and verified.", tempState->FStream->Name );
      tempState->FStream->Close();
      
      // Signal the main thread that the verification is finished.
      tempState->ManualEvent->Set();
   }


public:

   // When BeginWrite is finished writing data to the file, the
   // EndWriteCallback method is called to end the asynchronous 
   // write operation and then read back and verify the data.
   static void EndWriteCallback( IAsyncResult^ asyncResult )
   {
      State^ tempState = dynamic_cast<State^>(asyncResult->AsyncState);
      FileStream^ fStream = tempState->FStream;
      fStream->EndWrite( asyncResult );
      
      // Asynchronously read back the written data.
      fStream->Position = 0;
      asyncResult = fStream->BeginRead( tempState->ReadArray, 0, tempState->ReadArray->Length, gcnew AsyncCallback( &FStream::EndReadCallback ), tempState );
      
      // Concurrently do other work, such as 
      // logging the write operation.
   }

};


int main()
{
   
   // Create a synchronization object that gets 
   // signaled when verification is complete.
   ManualResetEvent^ manualEvent = gcnew ManualResetEvent( false );
   
   // Create the data to write to the file.
   array<Byte>^writeArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( writeArray );
   FileStream^ fStream = gcnew FileStream(  "Test#@@#.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::None,4096,true );
   
   // Check that the FileStream was opened asynchronously.
   Console::WriteLine( "fStream was {0}opened asynchronously.", fStream->IsAsync ? (String^)"" : "not " );
   
   // Asynchronously write to the file.
   IAsyncResult^ asyncResult = fStream->BeginWrite( writeArray, 0, writeArray->Length, gcnew AsyncCallback( &FStream::EndWriteCallback ), gcnew State( fStream,writeArray,manualEvent ) );
   
   // Concurrently do other work and then wait 
   // for the data to be written and verified.
   manualEvent->WaitOne( 5000, false );
}
using System;
using System.IO;
using System.Threading;

class FStream
{
    static void Main()
    {
        // Create a synchronization object that gets
        // signaled when verification is complete.
        ManualResetEvent manualEvent = new ManualResetEvent(false);

        // Create random data to write to the file.
        byte[] writeArray = new byte[100000];
        new Random().NextBytes(writeArray);

        FileStream fStream =
            new FileStream("Test#@@#.dat", FileMode.Create,
            FileAccess.ReadWrite, FileShare.None, 4096, true);

        // Check that the FileStream was opened asynchronously.
        Console.WriteLine("fStream was {0}opened asynchronously.",
            fStream.IsAsync ? "" : "not ");

        // Asynchronously write to the file.
        IAsyncResult asyncResult = fStream.BeginWrite(
            writeArray, 0, writeArray.Length,
            new AsyncCallback(EndWriteCallback),
            new State(fStream, writeArray, manualEvent));

        // Concurrently do other work and then wait
        // for the data to be written and verified.
        manualEvent.WaitOne(5000, false);
    }

    // When BeginWrite is finished writing data to the file, the
    // EndWriteCallback method is called to end the asynchronous
    // write operation and then read back and verify the data.
    static void EndWriteCallback(IAsyncResult asyncResult)
    {
        State tempState = (State)asyncResult.AsyncState;
        FileStream fStream = tempState.FStream;
        fStream.EndWrite(asyncResult);

        // Asynchronously read back the written data.
        fStream.Position = 0;
        asyncResult = fStream.BeginRead(
            tempState.ReadArray, 0 , tempState.ReadArray.Length,
            new AsyncCallback(EndReadCallback), tempState);

        // Concurrently do other work, such as
        // logging the write operation.
    }

    // When BeginRead is finished reading data from the file, the
    // EndReadCallback method is called to end the asynchronous
    // read operation and then verify the data.
    static void EndReadCallback(IAsyncResult asyncResult)
    {
        State tempState = (State)asyncResult.AsyncState;
        int readCount = tempState.FStream.EndRead(asyncResult);

        int i = 0;
        while(i < readCount)
        {
            if(tempState.ReadArray[i] != tempState.WriteArray[i++])
            {
                Console.WriteLine("Error writing data.");
                tempState.FStream.Close();
                return;
            }
        }
        Console.WriteLine("The data was written to {0} and verified.",
            tempState.FStream.Name);
        tempState.FStream.Close();

        // Signal the main thread that the verification is finished.
        tempState.ManualEvent.Set();
    }

    // Maintain state information to be passed to
    // EndWriteCallback and EndReadCallback.
    class State
    {
        // fStream is used to read and write to the file.
        FileStream fStream;

        // writeArray stores data that is written to the file.
        byte[] writeArray;

        // readArray stores data that is read from the file.
        byte[] readArray;

        // manualEvent signals the main thread
        // when verification is complete.
        ManualResetEvent manualEvent;

        public State(FileStream fStream, byte[] writeArray,
            ManualResetEvent manualEvent)
        {
            this.fStream   = fStream;
            this.writeArray = writeArray;
            this.manualEvent = manualEvent;
            readArray = new byte[writeArray.Length];
        }

        public FileStream FStream
        { get{ return fStream; } }

        public byte[] WriteArray
        { get{ return writeArray; } }

        public byte[] ReadArray
        { get{ return readArray; } }

        public ManualResetEvent ManualEvent
        { get{ return manualEvent; } }
    }
}
open System
open System.IO
open System.Threading

// Maintain state information to be passed to
// EndWriteCallback and EndReadCallback.
type State(fStream: FileStream, writeArray: byte[], manualEvent: ManualResetEvent) =
    // readArray stores data that is read from the file.
    let readArray = Array.zeroCreate writeArray.Length

    member _.FStream = fStream
    member _.WriteArray = writeArray
    member _.ReadArray = readArray
    member _.ManualEvent = manualEvent

// When BeginRead is finished reading data from the file, the
// EndReadCallback method is called to end the asynchronous
// read operation and then verify the data.
let endReadCallback (asyncResult: IAsyncResult) =
    let tempState = asyncResult.AsyncState :?> State
    let readCount = tempState.FStream.EndRead asyncResult

    let mutable i = 0
    let mutable errored = false

    while i < readCount do
        if tempState.ReadArray[i] <> tempState.WriteArray[i] then
            printfn "Error writing data."
            tempState.FStream.Close()
            errored <- true
            i <- readCount

        i <- i + 1

    printfn $"The data was written to {tempState.FStream.Name} and verified."
    tempState.FStream.Close()
    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set() |> ignore


// When BeginWrite is finished writing data to the file, the
// EndWriteCallback method is called to end the asynchronous
// write operation and then read back and verify the data.
let endWriteCallback (asyncResult: IAsyncResult) =
    let tempState = asyncResult.AsyncState :?> State
    let fStream = tempState.FStream
    fStream.EndWrite asyncResult

    // Asynchronously read back the written data.
    fStream.Position <- 0

    let asyncResult =
        fStream.BeginRead(tempState.ReadArray, 0, tempState.ReadArray.Length, AsyncCallback endReadCallback, tempState)

    // Concurrently do other work, such as
    // logging the write operation.
    ()


// Create a synchronization object that gets
// signaled when verification is complete.
let manualEvent = new ManualResetEvent false

// Create random data to write to the file.
let writeArray = Array.zeroCreate 100000
Random.Shared.NextBytes writeArray

let fStream =
    new FileStream("Test#@@#.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true)

// Check that the FileStream was opened asynchronously.

if fStream.IsAsync then "" else "not "
|> printfn "fStream was %sopened asynchronously."

// Asynchronously write to the file.
let asyncResult =
    fStream.BeginWrite(
        writeArray,
        0,
        writeArray.Length,
        AsyncCallback endWriteCallback,
        State(fStream, writeArray, manualEvent)
    )

// Concurrently do other work and then wait
// for the data to be written and verified.
manualEvent.WaitOne(5000, false) |> ignore
Imports System.IO
Imports System.Threading

Class FStream

    Shared Sub Main()

        ' Create a synchronization object that gets 
        ' signaled when verification is complete.
        Dim manualEvent As New ManualResetEvent(False)

        ' Create random data to write to the file.
        Dim writeArray(100000) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(writeArray)

        Dim fStream As New FileStream("Test#@@#.dat", _
            FileMode.Create, FileAccess.ReadWrite, _
            FileShare.None, 4096, True)

        ' Check that the FileStream was opened asynchronously.
        If fStream.IsAsync = True
            Console.WriteLine("fStream was opened asynchronously.")
        Else
            Console.WriteLine("fStream was not opened asynchronously.")
        End If

        ' Asynchronously write to the file.
        Dim asyncResult As IAsyncResult = fStream.BeginWrite( _
            writeArray, 0, writeArray.Length, _
            AddressOf EndWriteCallback , _
            New State(fStream, writeArray, manualEvent))

        ' Concurrently do other work and then wait
        ' for the data to be written and verified.
        manualEvent.WaitOne(5000, False)
    End Sub

    ' When BeginWrite is finished writing data to the file, the
    ' EndWriteCallback method is called to end the asynchronous 
    ' write operation and then read back and verify the data.
    Private Shared Sub EndWriteCallback(asyncResult As IAsyncResult)
        Dim tempState As State = _
            DirectCast(asyncResult.AsyncState, State)
        Dim fStream As FileStream = tempState.FStream
        fStream.EndWrite(asyncResult)

        ' Asynchronously read back the written data.
        fStream.Position = 0
        asyncResult = fStream.BeginRead( _ 
            tempState.ReadArray, 0 , tempState.ReadArray.Length, _
            AddressOf EndReadCallback, tempState)

        ' Concurrently do other work, such as 
        ' logging the write operation.
    End Sub

    ' When BeginRead is finished reading data from the file, the 
    ' EndReadCallback method is called to end the asynchronous 
    ' read operation and then verify the data.
   Private Shared Sub EndReadCallback(asyncResult As IAsyncResult)
        Dim tempState As State = _
            DirectCast(asyncResult.AsyncState, State)
        Dim readCount As Integer = _
            tempState.FStream.EndRead(asyncResult)

        Dim i As Integer = 0
        While(i < readCount)
            If(tempState.ReadArray(i) <> tempState.WriteArray(i))
                Console.WriteLine("Error writing data.")
                tempState.FStream.Close()
                Return
            End If
            i += 1
        End While

        Console.WriteLine("The data was written to {0} and " & _
            "verified.", tempState.FStream.Name)
        tempState.FStream.Close()

        ' Signal the main thread that the verification is finished.
        tempState.ManualEvent.Set()
    End Sub

    ' Maintain state information to be passed to 
    ' EndWriteCallback and EndReadCallback.
    Private Class State

        ' fStreamValue is used to read and write to the file.
        Dim fStreamValue As FileStream

        ' writeArrayValue stores data that is written to the file.
        Dim writeArrayValue As Byte()

        ' readArrayValue stores data that is read from the file.
        Dim readArrayValue As Byte()

        ' manualEvent signals the main thread 
        ' when verification is complete.
        Dim manualEventValue As ManualResetEvent 

        Sub New(aStream As FileStream, anArray As Byte(), _
            manualEvent As ManualResetEvent)

            fStreamValue     = aStream
            writeArrayValue  = anArray
            manualEventValue = manualEvent
            readArrayValue   = New Byte(anArray.Length - 1){}
        End Sub    

            Public ReadOnly Property FStream() As FileStream
                Get
                    Return fStreamValue
                End Get
            End Property

            Public ReadOnly Property WriteArray() As Byte()
                Get
                    Return writeArrayValue
                End Get
            End Property

            Public ReadOnly Property ReadArray() As Byte()
                Get
                    Return readArrayValue
                End Get
            End Property

            Public ReadOnly Property ManualEvent() As ManualResetEvent
                Get
                    Return manualEventValue
                End Get
            End Property
    End Class 
   
End Class

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileMode, FileAccess)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定したパス、作成モード、および読み取り/書き込みアクセス許可を使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode, System::IO::FileAccess access);
public FileStream (string path, System.IO.FileMode mode, System.IO.FileAccess access);
new System.IO.FileStream : string * System.IO.FileMode * System.IO.FileAccess -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode, access As FileAccess)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

access
FileAccess

FileStream オブジェクトがファイルにアクセスする方法を決定する列挙値のビットごとの組み合わせ。 これにより、FileStream オブジェクトの CanRead および CanWrite プロパティによって返される値も決まります。 path がディスク ファイルを指定している場合、CanSeektrue になります。

例外

pathnullです。

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

accessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する access 要求がオペレーティング システムで許可されません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

mode に無効な値が含まれています。

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

パラメーターには path 、汎用名前付け規則 (UNC) 共有のファイルを含むファイル名を指定できます。

コンストラクターには、ファイルに対する読み取り/書き込みアクセス権が付与され、読み取りアクセス権を共有して開かれます (つまり、この方法でファイルを書き込むための要求、またはオブジェクトが閉じられるまで FileStream 別のプロセスを開く要求は失敗しますが、読み取り試行は成功します)。 バッファー サイズは、既定のサイズの 4096 バイト (4 KB) に設定されます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクトに対してです。 シークをサポートしていないデバイスを示す場合path、結果FileStreamCanSeek の プロパティは になりますfalse。 詳細については、「CanSeek」を参照してください。

FileShare.Read は、パラメーターを持たないコンストラクター FileStream の既定値です FileShare

注意事項

特定のカルチャ設定の文字セットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(IntPtr, FileAccess, Boolean)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202

注意事項

This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. http://go.microsoft.com/fwlink/?linkid=14202

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可と FileStream インスタンスの所有権を使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(IntPtr handle, System::IO::FileAccess access, bool ownsHandle);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  https://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle);
[System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle);
public FileStream (IntPtr handle, System.IO.FileAccess access, bool ownsHandle);
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  https://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) and optionally make a new SafeFileHandle with ownsHandle=false if needed instead.")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed.  http://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool -> System.IO.FileStream
new System.IO.FileStream : nativeint * System.IO.FileAccess * bool -> System.IO.FileStream
Public Sub New (handle As IntPtr, access As FileAccess, ownsHandle As Boolean)

パラメーター

handle
IntPtr

nativeint

現在の FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead および CanWrite プロパティを設定する列挙値のビットごとの組み合わせ。

ownsHandle
Boolean

この FileStream インスタンスがファイル ハンドルを所有する場合は true。それ以外の場合は false

属性

例外

accessFileAccess のフィールドではありません。

呼び出し元に、必要なアクセス許可がありません。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

オブジェクトには FileStream 、指定したファイルへのアクセス権が付与されます。 ハンドルの所有権は、指定したとおりになります。 このプロセスがハンドルを所有している場合、 メソッドの Close 呼び出しもハンドルを閉じ、ファイルのハンドル数がデクリメントされます。 オブジェクトには FileStream 、4096 バイトの既定のバッファー サイズが指定されます。

FileStream は、ハンドルを排他的に制御していることを前提としています。 がハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。

FileShare.Read は、パラメーターを持たないコンストラクター FileStream の既定値です FileShare

注意事項

特定のカルチャ設定の文字セットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(SafeFileHandle, FileAccess, Int32)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、およびバッファー サイズを使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(Microsoft::Win32::SafeHandles::SafeFileHandle ^ handle, System::IO::FileAccess access, int bufferSize);
public FileStream (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.IO.FileAccess access, int bufferSize);
new System.IO.FileStream : Microsoft.Win32.SafeHandles.SafeFileHandle * System.IO.FileAccess * int -> System.IO.FileStream
Public Sub New (handle As SafeFileHandle, access As FileAccess, bufferSize As Integer)

パラメーター

handle
SafeFileHandle

現在の FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead プロパティと CanWrite プロパティを設定する FileAccess 定数。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

例外

handle パラメーターが無効なハンドルです。

または

同期ハンドルである handle パラメーターが、非同期的に使用されました。

bufferSize パラメーターが負の値です。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

FileStream は、ハンドルを排他的に制御していることを前提としています。 がハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。 または、この FileStream コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。

FileShare.Read は、パラメーターを持たないコンストラクター FileStream の既定値です FileShare

注意事項

特定のカルチャ設定の文字セットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(SafeFileHandle, FileAccess, Int32, Boolean)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、バッファー サイズ、および同期状態または非同期状態を使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(Microsoft::Win32::SafeHandles::SafeFileHandle ^ handle, System::IO::FileAccess access, int bufferSize, bool isAsync);
public FileStream (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.IO.FileAccess access, int bufferSize, bool isAsync);
new System.IO.FileStream : Microsoft.Win32.SafeHandles.SafeFileHandle * System.IO.FileAccess * int * bool -> System.IO.FileStream
Public Sub New (handle As SafeFileHandle, access As FileAccess, bufferSize As Integer, isAsync As Boolean)

パラメーター

handle
SafeFileHandle

この FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead および CanWrite プロパティを設定する列挙値のビットごとの組み合わせ。

bufferSize
Int32

バッファー サイズを示す 0 より大きな正の Int32 値。 既定のバッファー サイズは、4096 です。

isAsync
Boolean

ハンドルが非同期的に開かれた場合 (重複 I/O モード用) は true。それ以外の場合は false

例外

handle パラメーターが無効なハンドルです。

または

同期ハンドルである handle パラメーターが、非同期的に使用されました。

bufferSize パラメーターが負の値です。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

呼び出し元に、必要なアクセス許可がありません。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

ファイル ハンドルを isAsync 非同期で開くには、 パラメーターを に true 設定します。 パラメーターが の true場合、ストリームは重複した I/O を使用してファイル操作を非同期的に実行します。 ただし、 パラメーターは、または CopyToAsync メソッドをWriteAsyncReadAsync呼び出す必要trueはありません。 パラメーターが isAsyncfalse 、非同期の読み取りおよび書き込み操作を呼び出しても、UI スレッドはブロックされませんが、実際の I/O 操作は同期的に実行されます。

FileStream は、ハンドルを排他的に制御していることを前提としています。 がハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。 または、この FileStream コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。

FileShare.Read は、パラメーターを持たないコンストラクター FileStream の既定値です FileShare

注意事項

特定のカルチャ設定の文字セットをコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(String, FileStreamOptions)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

指定したパス、作成モード、読み取り/書き込みおよび共有アクセス許可、バッファー サイズ、追加のファイル オプション、事前割り当てサイズ、および他の FileStreams が同じファイルに対して持つアクセス権を使用して、 クラスの新しいインスタンス FileStream を初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileStreamOptions ^ options);
public FileStream (string path, System.IO.FileStreamOptions options);
new System.IO.FileStream : string * System.IO.FileStreamOptions -> System.IO.FileStream
Public Sub New (path As String, options As FileStreamOptions)

パラメーター

path
String

現在 FileStream のインスタンスがカプセル化するファイルの相対パスまたは絶対パス。

options
FileStreamOptions

使用する省略可能な FileStream パラメーターを記述する オブジェクト。

例外

path または optionsnull です。

path は空の文字列、空白のみを含む、または 1 つ以上の無効な文字を含みます。

または

pathは、NTFS 環境のファイル以外のデバイス (、 COM1:LPT1:などCON:) を参照します。

pathは、NTFS 以外の環境のファイル以外のデバイス (、 などCON:COM1:LPT1:) を指します。

ファイルが見つかりません。たとえば、ModeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

または

ディスクがいっぱいになりました (提供され、path通常のファイルを指していた場合PreallocationSize)。

または

ファイルが大きすぎます (指定され、path通常のファイルを指していた場合PreallocationSize)。

呼び出し元に、必要なアクセス許可がありません。

マップされていないドライブにあるなど、指定されたパスが無効です。

AccessWrite または ReadWrite であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path に対する Access 要求がオペレーティング システムで許可されません。

または

Encrypted は に Options 指定されていますが、現在のプラットフォームではファイル暗号化はサポートされていません。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

注釈

の値 PreallocationSize を指定すると、事前割り当てサイズに関するヒントが提供されますが、強力な保証はありません。 使用の詳細については、 のドキュメントを参照してください PreallocationSize

適用対象

FileStream(String, FileMode)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

FileStream クラスの新しいインスタンスを、指定したパスと作成モードを使用して初期化します。

public:
 FileStream(System::String ^ path, System::IO::FileMode mode);
public FileStream (string path, System.IO.FileMode mode);
new System.IO.FileStream : string * System.IO.FileMode -> System.IO.FileStream
Public Sub New (path As String, mode As FileMode)

パラメーター

path
String

現在の FileStream オブジェクトによってカプセル化されるファイルの相対パスまたは絶対パス。

mode
FileMode

ファイルを開く方法または作成する方法を決定する列挙値の 1 つ。

例外

.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path は空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。

または

path が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。

path がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。

pathnullです。

呼び出し元に、必要なアクセス許可がありません。

ファイルが見つかりません。たとえば、modeFileMode.Truncate または FileMode.Open の場合に、path で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。

path は読み取り専用のファイルを指定します。

path で指定したファイルが既に存在している場合に FileMode.CreateNew が指定されているなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

マップされていないドライブにあるなど、指定されたパスが無効です。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

mode に無効な値が含まれています。

次のコード例は、データをファイルにバイト単位で書き込み、データが正しく書き込まれたことを確認する方法を示しています。

using namespace System;
using namespace System::IO;
int main()
{
   String^ fileName =  "Test@##@.dat";
   
   // Create random data to write to the file.
   array<Byte>^dataArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( dataArray );
   FileStream^ fileStream = gcnew FileStream( fileName,FileMode::Create );
   try
   {
      
      // Write the data to the file, byte by byte.
      for ( int i = 0; i < dataArray->Length; i++ )
      {
         fileStream->WriteByte( dataArray[ i ] );

      }
      
      // Set the stream position to the beginning of the file.
      fileStream->Seek( 0, SeekOrigin::Begin );
      
      // Read and verify the data.
      for ( int i = 0; i < fileStream->Length; i++ )
      {
         if ( dataArray[ i ] != fileStream->ReadByte() )
         {
            Console::WriteLine( "Error writing data." );
            return  -1;
         }

      }
      Console::WriteLine( "The data was written to {0} "
      "and verified.", fileStream->Name );
   }
   finally
   {
      fileStream->Close();
   }

}
using System;
using System.IO;

class FStream
{
    static void Main()
    {
        const string fileName = "Test#@@#.dat";

        // Create random data to write to the file.
        byte[] dataArray = new byte[100000];
        new Random().NextBytes(dataArray);

        using(FileStream
            fileStream = new FileStream(fileName, FileMode.Create))
        {
            // Write the data to the file, byte by byte.
            for(int i = 0; i < dataArray.Length; i++)
            {
                fileStream.WriteByte(dataArray[i]);
            }

            // Set the stream position to the beginning of the file.
            fileStream.Seek(0, SeekOrigin.Begin);

            // Read and verify the data.
            for(int i = 0; i < fileStream.Length; i++)
            {
                if(dataArray[i] != fileStream.ReadByte())
                {
                    Console.WriteLine("Error writing data.");
                    return;
                }
            }
            Console.WriteLine("The data was written to {0} " +
                "and verified.", fileStream.Name);
        }
    }
}
open System
open System.IO


let fileName = "Test#@@#.dat"

// Create random data to write to the file.
let dataArray = Array.zeroCreate 100000
Random.Shared.NextBytes dataArray

do
    use fileStream = new FileStream(fileName, FileMode.Create)
    // Write the data to the file, byte by byte.
    for i = 0 to dataArray.Length - 1 do
        fileStream.WriteByte dataArray[i]

    // Set the stream position to the beginning of the file.
    fileStream.Seek(0, SeekOrigin.Begin) |> ignore

    // Read and verify the data.
    for i in 0L .. fileStream.Length - 1L do
        if dataArray[int i] <> (fileStream.ReadByte() |> byte) then
            printfn "Error writing data."
            exit 1

    printfn $"The data was written to {fileStream.Name} and verified."
Imports System.IO
Imports System.Text

Class FStream

    Shared Sub Main()

        Const fileName As String = "Test#@@#.dat"

        ' Create random data to write to the file.
        Dim dataArray(100000) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataArray)

        Dim fileStream As FileStream = _
            new FileStream(fileName, FileMode.Create)
        Try

            ' Write the data to the file, byte by byte.
            For i As Integer = 0 To dataArray.Length - 1
                fileStream.WriteByte(dataArray(i))
            Next i

            ' Set the stream position to the beginning of the stream.
            fileStream.Seek(0, SeekOrigin.Begin)

            ' Read and verify the data.
            For i As Integer = 0 To _
                CType(fileStream.Length, Integer) - 1

                If dataArray(i) <> fileStream.ReadByte() Then
                    Console.WriteLine("Error writing data.")
                    Return
                End If
            Next i
            Console.WriteLine("The data was written to {0} " & _
                "and verified.", fileStream.Name)
        Finally
            fileStream.Close()
        End Try
    
    End Sub
End Class

注釈

.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。

パラメーターには path 、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。

コンストラクターには、ファイルへの読み取り/書き込みアクセス権が与えられ、読み取りアクセス権を共有して開かれます (つまり、このまたは別のプロセスによる書き込みのためにファイルを開く要求は、オブジェクトが閉じられるまで FileStream 失敗しますが、読み取り試行は成功します)。

このコンストラクターを使用して読み取り専用ファイルを開くことはできません。代わりに、 値が に設定されたパラメーターを FileAccess 受け取るコンストラクターを使用する FileAccess.Read必要があります。

バッファー サイズは、既定のサイズである 4096 バイト (4 KB) に設定されます。

Note

path は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。

CanSeek は、 true ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path、結果の FileStream の プロパティは になりますfalseCanSeek。 詳細については、「CanSeek」を参照してください。

FileShare.Read は、パラメーターのないコンストラクター FileStream の既定値です FileShare

パラメーターを持たないコンストラクターのFileAccess場合、 パラメーターが modeAppendWrite設定されている場合は、既定のアクセスが になります。 それ以外の場合、アクセスは に ReadWrite設定されます。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

FileStream(IntPtr, FileAccess)

ソース:
FileStream.cs
ソース:
FileStream.cs
ソース:
FileStream.cs

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead. https://go.microsoft.com/fwlink/?linkid=14202

注意事項

This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) instead.

注意事項

This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202

FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可を使用して、指定したファイル ハンドル用に初期化します。

public:
 FileStream(IntPtr handle, System::IO::FileAccess access);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead.  https://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access);
[System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) instead.")]
public FileStream (IntPtr handle, System.IO.FileAccess access);
[System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead.  http://go.microsoft.com/fwlink/?linkid=14202")]
public FileStream (IntPtr handle, System.IO.FileAccess access);
public FileStream (IntPtr handle, System.IO.FileAccess access);
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead.  https://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated. Use FileStream(SafeFileHandle handle, FileAccess access) instead.")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess -> System.IO.FileStream
[<System.Obsolete("This constructor has been deprecated.  Please use new FileStream(SafeFileHandle handle, FileAccess access) instead.  http://go.microsoft.com/fwlink/?linkid=14202")>]
new System.IO.FileStream : nativeint * System.IO.FileAccess -> System.IO.FileStream
new System.IO.FileStream : nativeint * System.IO.FileAccess -> System.IO.FileStream
Public Sub New (handle As IntPtr, access As FileAccess)

パラメーター

handle
IntPtr

nativeint

現在の FileStream オブジェクトによってカプセル化されるファイルのファイル ハンドル。

access
FileAccess

FileStream オブジェクトの CanRead および CanWrite プロパティを設定する列挙値のビットごとの組み合わせ。

属性

例外

accessFileAccess のフィールドではありません。

呼び出し元に、必要なアクセス許可がありません。

ディスク エラーなどの I/O エラーが発生しました。

または

ストリームは閉じられました。

accessWrite または ReadWrite であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access 要求がオペレーティング システムで許可されません。

注釈

が呼び出されると Close 、ハンドルも閉じられ、ファイルのハンドル数がデクリメントされます。

FileStream は、ハンドルを排他的に制御していることを前提としています。 ハンドルを保持している間 FileStream に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するには、ハンドルを使用する前に を呼び出 Flush し、ハンドルの使用が完了した後以外 Close のメソッドを呼び出さないようにします。

注意事項

特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、その文字が解釈できず、例外がスローされる可能性があります。

FileShare.Read は、パラメーターのないコンストラクター FileStream の既定値です FileShare

一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象