FileStream コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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(String, FileMode, FileAccess, FileShare) |
FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、および共有アクセス許可を使用して初期化します。 |
FileStream(IntPtr, FileAccess, Boolean, Int32) |
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。
FileStream クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可、 |
FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) |
FileStream クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、バッファー サイズ、および同期状態または非同期状態を使用して初期化します。 |
FileStream(String, FileMode, FileAccess) |
FileStream クラスの新しいインスタンスを、指定したパス、作成モード、および読み取り/書き込みアクセス許可を使用して初期化します。 |
FileStream(IntPtr, FileAccess, Boolean) |
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。
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 クラスの新しいインスタンスを、指定した読み取り/書き込みアクセス許可を使用して、指定したファイル ハンドル用に初期化します。
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 プロパティを設定する列挙値のビットごとの組み合わせ。
例外
access
が FileAccess のフィールドではありません。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または 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
ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。
- options
- FileOptions
その他のファイル オプションを指定する列挙値のビットごとの組み合わせ。
- fileSecurity
- FileSecurity
ファイルのアクセス制御と監査セキュリティを決定するオブジェクト。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または無効な文字が 1 つ以上含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または 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();
}
}
}
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 コンストラクターを使用して、ファイルの作成時にアクセス権を適用します。 既存のファイルの権限にアクセスまたは変更するには、and SetAccessControl メソッドの使用をGetAccessControl検討してください。
この fileOptions
パラメーターは、オブジェクトの作成時に利用できるより高度な操作へのアクセスを FileStream 提供するために使用されます。
パラメーターには path
、汎用名前付け規則 (UNC) 共有上のファイルを含むファイル名を指定できます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分を指定できます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「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
ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。
- options
- FileOptions
その他のファイル オプションを指定する列挙値のビットごとの組み合わせ。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
現在のオペレーティング システムは Windows NT 以降ではありません。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または ReadWrite
であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path
に対する access
要求がオペレーティング システムで許可されません。
または
options
に対して Encrypted が指定されていますが、ファイルの暗号化は現在のプラットフォームではサポートされていません。
指定した path
、ファイル名、またはその両方がシステム定義の最大長を超えています。
注釈
.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。
この FileStream コンストラクターを使用して、ファイルの作成時にアクセス権を適用します。 既存のファイルに対する権限にアクセスまたは変更するには、and SetAccessControl メソッドの使用をGetAccessControl検討してください。
この fileOptions
パラメーターは、オブジェクトの作成時に利用できるより高度な操作へのアクセスを FileStream 提供するために使用されます。
このパラメーターには path
、汎用名前付け規則 (UNC) 共有のファイルを含むファイル名を指定できます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)
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
がディスク ファイルを指定している場合、CanSeek は true
になります。
- share
- FileShare
ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。
- options
- FileOptions
その他のファイル オプションを指定する列挙値のビットごとの組み合わせ。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または 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();
}
}
}
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) 共有のファイルを含むファイル名を指定できます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(String, FileMode, FileAccess, FileShare, Int32)
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
がディスク ファイルを指定している場合、CanSeek は true
になります。
- share
- FileShare
ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または ReadWrite
であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path
に対する access
要求がオペレーティング システムで許可されません。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
注釈
.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。
このパラメーターには path
、汎用名前付け規則 (UNC) 共有のファイルを含むファイル名を指定できます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)
注意事項
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
注意事項
Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead
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("Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) 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. 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
[<System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead")>]
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
。
- isAsync
- Boolean
ハンドルが非同期的に開かれた場合 (重複 I/O モード用) は true
。それ以外の場合は false
。
- 属性
例外
access
が FileAccess.Read
未満であるか、FileAccess.ReadWrite
以上であるか、または bufferSize
が 0 以下です。
ハンドルが正しくありません。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または ReadWrite
であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access
要求がオペレーティング システムで許可されません。
注釈
オブジェクトには FileStream
、指定されたファイルへのアクセス権が付与されます。 ハンドルの所有権は、指定されたとおりになります。 これが FileStream
ハンドルを所有している場合、メソッドの Close 呼び出しもハンドルを閉じます。 特に、ファイルのハンドル数はデクリメントされます。 オブジェクトには FileStream
、指定されたバッファー サイズが与えられます。
FileStream
は、ハンドルを排他的に制御することを前提としています。 ハンドルを保持している間 FileStream
に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するために、ハンドルを使用する前に呼び出 Flush し、ハンドルの使用が完了した後以外 Close
のメソッドを呼び出さないようにします。 または、この FileStream
コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(String, FileMode, FileAccess, FileShare)
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
がディスク ファイルを指定している場合、CanSeek は true
になります。
- share
- FileShare
ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または 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))
Dim aFileStream As New FileStream( _
"Test#@@#.dat", FileMode.OpenOrCreate, _
FileAccess.ReadWrite, FileShare.ReadWrite)
注釈
.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。
このパラメーターには path
、汎用名前付け規則 (UNC) 共有のファイルを含むファイル名を指定できます。
コンストラクターには、ファイルへの読み取り/書き込みアクセス権が付与され、共有読み取りアクセス権が開かれます (つまり、このプロセスで書き込むためのファイルを開く要求は、オブジェクトが閉じられるまで FileStream
失敗しますが、読み取り試行は成功します)。 バッファー サイズは、4096 バイト (4 KB) の既定のサイズに設定されます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(IntPtr, FileAccess, Boolean, Int32)
注意事項
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
注意事項
Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead
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("Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) 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. 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
[<System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead")>]
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
が負の値です。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または 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 クラスの新しいインスタンスを、指定したパス、作成モード、読み取り/書き込みアクセス許可、共有アクセス許可、バッファー サイズ、および同期状態または非同期状態を使用して初期化します。
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
がディスク ファイルを指定している場合、CanSeek は true
になります。
- share
- FileShare
ファイルがどのようにプロセスに共有されるかを決定する列挙値のビットごとの組み合わせ。
- 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 ほど低下することがあります。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または ReadWrite
であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path
に対する access
要求がオペレーティング システムで許可されません。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
例
次のコード例は、非同期的にデータをファイルに書き込み、データが正しく書き込まれたことを確認する方法を示しています。 State
メイン スレッドからメソッドに情報を渡すオブジェクトがEndReadCallback``EndWriteCallback
作成されます。
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; } }
}
}
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) 共有のファイルを含むファイル名を指定できます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(String, FileMode, FileAccess)
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
がディスク ファイルを指定している場合、CanSeek は true
になります。
例外
path
が null
です。
.NET Frameworkおよび .NET Core バージョンが 2.1 より前の場合: path
空の文字列 ("")、空白のみを含む、または 1 つ以上の無効な文字が含まれています。
または
path
が NTFS 環境の "con:"、"com1:"、"lpt1:" などの非ファイル デバイスを参照しています。
path
がファイル以外のデバイスを参照しています (NTFS 以外の環境の "con:"、"com1:"、"lpt1:" など)。
ファイルが見つかりません。たとえば、mode
が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または ストリームは閉じられました。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
access
が Write
または ReadWrite
であるのに、ファイルまたはディレクトリが読み取り専用に設定されているなど、指定した path
に対する access
要求がオペレーティング システムで許可されません。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
mode
に無効な値が含まれています。
注釈
.NET Frameworkでは、"\\.\PHYSICALDRIVE0" などのデバイス名であるパスを介した物理ディスクへの直接アクセスはサポートされていません。
このパラメーターには path
、汎用名前付け規則 (UNC) 共有のファイルを含むファイル名を指定できます。
コンストラクターには、ファイルへの読み取り/書き込みアクセス権が付与され、共有読み取りアクセス権が開かれます (つまり、このプロセスで書き込むためのファイルを開く要求は、オブジェクトが閉じられるまで FileStream
失敗しますが、読み取り試行は成功します)。 バッファー サイズは、4096 バイト (4 KB) の既定のサイズに設定されます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(IntPtr, FileAccess, Boolean)
注意事項
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
注意事項
Use FileStream(SafeFileHandle handle, FileAccess access) instead
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("Use FileStream(SafeFileHandle handle, FileAccess access) 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. 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
[<System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access) instead")>]
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
。
- 属性
例外
access
が FileAccess のフィールドではありません。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または ReadWrite
であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access
要求がオペレーティング システムで許可されません。
注釈
オブジェクトには FileStream
、指定したファイルへのアクセス権が付与されます。 ハンドルの所有権は、指定したとおりになります。 このプロセスがハンドルを所有している場合、メソッドの Close 呼び出しもハンドルを閉じ、ファイルのハンドル数が減少します。 オブジェクトの FileStream
既定のバッファー サイズは 4096 バイトです。
FileStream
は、ハンドルを排他的に制御することを前提としています。 ハンドルを保持している間 FileStream
に読み取り、書き込み、シークを行うと、データが破損する可能性があります。 データの安全性を確保するために、ハンドルを使用する前に呼び出 Flush し、ハンドルの使用が完了した後以外 Close
のメソッドの呼び出しは避けてください。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
注意事項
特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(SafeFileHandle, FileAccess, Int32)
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
パラメーターが負の値です。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または ReadWrite
であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access
要求がオペレーティング システムで許可されません。
注釈
FileStream
は、ハンドルを排他的に制御することを前提としています。 ハンドルを保持している間 FileStream
に読み取り、書き込み、シークを行うと、データが破損する可能性があります。 データの安全性を確保するために、ハンドルを使用する前に呼び出 Flush し、ハンドルの使用が完了した後以外 Close
のメソッドを呼び出さないようにします。 または、この FileStream
コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
注意事項
特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(SafeFileHandle, FileAccess, Int32, Boolean)
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 プロパティを設定する列挙値のビットごとの組み合わせ。
- isAsync
- Boolean
ハンドルが非同期的に開かれた場合 (重複 I/O モード用) は true
。それ以外の場合は false
。
例外
bufferSize
パラメーターが負の値です。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または ReadWrite
であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access
要求がオペレーティング システムで許可されません。
注釈
ファイル ハンドルをisAsync``true
非同期で開くパラメーターを設定します。 パラメーターが指定されている場合、ストリームは true
重複した I/O を使用してファイル操作を非同期的に実行します。 ただし、パラメーターは 、WriteAsync、またはCopyToAsyncメソッドをReadAsync呼び出す必要true
はありません。 パラメーターがisAsync``false
指定されていて、非同期の読み取りおよび書き込み操作を呼び出しても、UI スレッドはブロックされませんが、実際の I/O 操作は同期的に実行されます。
FileStream
は、ハンドルを排他的に制御することを前提としています。 ハンドルを保持している間 FileStream
に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するために、ハンドルを使用する前に呼び出 Flush し、ハンドルの使用が完了した後以外 Close
のメソッドを呼び出さないようにします。 または、この FileStream
コンストラクターを呼び出す前に、ハンドルの読み取りと書き込みを行います。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(String, FileStreamOptions)
指定したパス、作成モード、読み取り/書き込みおよび共有のアクセス許可、バッファー サイズ、追加のファイル オプション、事前割り当てサイズ、および他の 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
または options
が null
です。
path
は空の文字列、空白のみを含む文字列、または 1 つ以上の無効な文字を含みます。
または
path
は、NTFS 環境の CON:
、 COM1:
、 LPT1:
などのファイル以外のデバイスを指します。
path
は、非 NTFS 環境の CON:
、 COM1:
、 LPT1:
など、ファイル以外のデバイスを指します。
ファイルが見つかりません。たとえば、Mode が FileMode.Truncate
または FileMode.Open
の場合に、path
で指定されたファイルが存在しません。 これらのモードではファイルが既に存在している必要があります。
path
で指定したファイルが既に存在している場合に FileMode.CreateNew
が指定されているなどの I/O エラーが発生しました。
または
ストリームは閉じられました。
または
ディスクがいっぱいでした (指定され、path
通常のファイルを指していた場合PreallocationSize)。
または
ファイルが大きすぎます (指定され、path
通常のファイルを指していた場合PreallocationSize)。
呼び出し元に、必要なアクセス許可がありません。
マップされていないドライブにあるなど、指定されたパスが無効です。
Access要求されたオペレーティング システムでは、指定path
されたオペレーティング システムでは、指定された場合や、ファイルまたはReadWriteディレクトリがWrite読み取り専用アクセス用に設定されている場合Accessなど、許可されません。
または
Encrypted は指定されていますが、現在の Options プラットフォームではファイル暗号化はサポートされていません。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
注釈
値 PreallocationSize を指定すると、事前割り当てサイズに関するヒントが提供されますが、強力な保証はありません。 使用の詳細については、次のドキュメント PreallocationSizeを参照してください。
適用対象
FileStream(String, FileMode)
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:" など)。
path
が null
です。
呼び出し元に、必要なアクセス許可がありません。
ファイルが見つかりません。たとえば、mode
が FileMode.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);
}
}
}
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) の既定のサイズに設定されます。
注意
path
は、ディスクに格納されているファイルである必要はありません。ストリーム経由のアクセスをサポートするシステムの任意の部分にすることができます。 たとえば、システムによっては、このクラスは物理デバイスにアクセスできます。
CanSeek は、 true
ファイルをカプセル化するすべての FileStream オブジェクト用です。 シークをサポートしていないデバイスを示す場合path
、結果FileStreamのCanSeekプロパティは false
. 詳細については、「CanSeek」を参照してください。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
パラメーターのないコンストラクターの FileAccess 場合、パラメーターが mode
設定 Appendされている場合は、 Write 既定のアクセスです。 それ以外の場合、アクセスは ReadWrite.
注意事項
特定のカルチャ設定で一連の文字をコンパイルし、異なるカルチャ設定で同じ文字を取得すると、文字が解釈できず、例外がスローされる可能性があります。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。
こちらもご覧ください
適用対象
FileStream(IntPtr, FileAccess)
注意事項
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
注意事項
Use FileStream(SafeFileHandle handle, FileAccess access) instead
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("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. 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
[<System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access) instead")>]
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 プロパティを設定する列挙値のビットごとの組み合わせ。
- 属性
例外
access
が FileAccess のフィールドではありません。
呼び出し元に、必要なアクセス許可がありません。
access
が Write
または ReadWrite
であるのに、ファイル ハンドルが読み取り専用に設定されているなど、指定したファイル ハンドルに対する access
要求がオペレーティング システムで許可されません。
注釈
呼び出されると Close 、ハンドルも閉じられ、ファイルのハンドル数が減少します。
FileStream
は、ハンドルを排他的に制御することを前提としています。 ハンドルを保持している間 FileStream
に読み取り、書き込み、またはシークを行うと、データが破損する可能性があります。 データの安全性を確保するために、ハンドルを使用する前に呼び出 Flush し、ハンドルの使用が完了した後以外 Close
のメソッドを呼び出さないようにします。
注意事項
特定のカルチャ設定を持つ文字のセットをコンパイルし、異なるカルチャ設定を持つ同じ文字を取得すると、文字が解釈できない可能性があり、例外がスローされる可能性があります。
FileShare.Read
は、パラメーターのないコンストラクター FileStream の既定値です FileShare
。
一般的なファイル操作とディレクトリ操作の一覧については、「 一般的な I/O タスク」を参照してください。