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 은 읽기 전용 파일 또는 디렉터리를 지정했습니다.

.NET Framework 및 .NET Core 버전 2.1 이전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. 메서드를 사용하여 잘못된 문자를 쿼리할 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 OpenWrite method")을 덮어쓰는 경우 파일에는 문자열 혼합("OpenWrite 메서드의 두 번째 runtest")이 포함됩니다.

매개 변수는 path 상대 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 메서드를 GetCurrentDirectory 사용합니다.

반환 FileStream 된 항목은 읽기를 지원하지 않습니다. 읽기 및 쓰기 모두에 대한 파일을 열려면 .를 사용합니다 Open.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

적용 대상

추가 정보