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

FileStream 액세스 권한이 있는 지정된 경로에서 공유되지 않은 Write 개체입니다.

예외

호출자에게 필요한 권한이 없는 경우

또는 path 읽기 전용 파일 또는 디렉터리를 지정했습니다.

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars() 메서드를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

path이(가) null인 경우

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

path의 형식이 잘못되었습니다.

예제

다음 예제에서는 읽고 쓰기 위한 파일을 엽니다.

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";

   // Open the stream and write to it.
   {
      FileStream^ fs = File::OpenWrite( path );
      try
      {
         array<Byte>^info = (gcnew UTF8Encoding( true ))->
            GetBytes( "This is to test the OpenWrite method." );

         // Add some information to the file.
         fs->Write( info, 0, info->Length );
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }

   // Open the stream and read it back.
   {
      FileStream^ fs = File::OpenRead( path );
      try
      {
         array<Byte>^b = gcnew array<Byte>(1024);
         UTF8Encoding^ temp = gcnew UTF8Encoding( true );
         while ( fs->Read( b, 0, b->Length ) > 0 )
         {
            Console::WriteLine( temp->GetString( b ) );
         }
      }
      finally
      {
         if ( fs )
            delete(IDisposable^)fs;
      }
   }
}
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));
            }
        }
    }
}
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 작업을 참조하세요.

적용 대상

추가 정보