File.OpenWrite(String) メソッド

定義

既存のファイルを開くか、書き込み用の新しいファイルを作成します。

public:
 static System::IO::FileStream ^ OpenWrite(System::String ^ path);
public static System.IO.FileStream OpenWrite(string path);
static member OpenWrite : string -> System.IO.FileStream
Public Shared Function OpenWrite (path As String) As FileStream

パラメーター

path
String

書き込み用に開くファイル。

返品

FileStream アクセス権を持つ、指定したパス上の共有されていないWrite オブジェクト。

例外

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

-又は-

path は、読み取り専用のファイルまたはディレクトリを指定しました。

2.1 より前のバージョンの .NET Framework と .NET Core: path は長さ 0 の文字列で、空白のみを含むか、1 つ以上の無効な文字を含みます。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

pathnullです。

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

指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

path が無効な形式です。

次の例では、読み取りと書き込み用にファイルを開きます。

using System;
using System.IO;
using System.Text;

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

        // Open the stream and write to it.
        using (FileStream fs = File.OpenWrite(path))
        {
            Byte[] info =
                new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");

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

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

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

let path = @"c:\temp\MyTest.txt"

// Open the stream and write to it.
do
    use fs = File.OpenWrite path

    let info =
        UTF8Encoding(true)
            .GetBytes "This is to test the OpenWrite method."

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

// Open the stream and read it back.
do
    use fs = File.OpenRead path
    let b = Array.zeroCreate 1024
    let temp = UTF8Encoding true

    while fs.Read(b, 0, b.Length) > 0 do
        printfn $"{temp.GetString b}"
Imports System.IO
Imports System.Text

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

    ' Open the stream and write to it.
    Using fs As FileStream = File.OpenWrite(path)
      Dim info As Byte() = _
       New UTF8Encoding(True).GetBytes("This is to test the OpenWrite method.")

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

    'Open the stream and read it back.
    Using fs As FileStream = File.OpenRead(path)
      Dim b(1023) As Byte
      Dim temp As UTF8Encoding = New UTF8Encoding(True)

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

  End Sub
End Class

注釈

このメソッドは、ファイル モードが FileStream(String, FileMode, FileAccess, FileShare) に設定され、アクセスが OpenOrCreate に設定され、共有モードが Write に設定されたNone コンストラクターのオーバーロードと同じです。

OpenWrite メソッドは、ファイル パスに既に存在する場合はファイルを開き、存在しない場合は新しいファイルを作成します。 既存のファイルの場合、新しいテキストは既存のテキストに追加されません。 代わりに、既存の文字を新しい文字で上書きします。 長い文字列 ("This is a test of the OpenWrite method"など) を短い文字列 ("Second run" など) で上書きした場合、ファイルには文字列が混在します ("OpenWrite メソッドの Second runtest")。

pathパラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectory メソッドを使用します。

返された FileStream は読み取りをサポートしていません。 読み取りと書き込みの両方のファイルを開くには、 Openを使用します。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

適用対象

こちらもご覧ください