File.Create 메서드

정의

지정된 경로에 파일을 만들거나 자르거나 덮어씁니다.

오버로드

Create(String)

지정된 경로에서 파일을 만들거나 자르거나 덮어씁니다.

Create(String, Int32)

버퍼 크기를 지정하여 지정된 경로의 파일을 만들거나 자르거나 덮어씁니다.

Create(String, Int32, FileOptions)

지정된 경로에 파일을 만들거나 덮어써서 버퍼 크기와 파일을 만들거나 덮어쓰는 방법을 설명하는 옵션을 지정합니다.

Create(String, Int32, FileOptions, FileSecurity)

지정된 경로에 파일을 만들거나 덮어써서 버퍼 크기, 파일을 만들거나 덮어쓰는 방법을 설명하는 옵션 그리고 파일에 대한 액세스 제어 및 감사 보안을 결정하는 값을 지정합니다.

Create(String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

지정된 경로에서 파일을 만들거나 자르거나 덮어씁니다.

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

매개 변수

path
String

만들 파일의 경로와 이름입니다.

반환

FileStream에 지정된 파일에 대한 읽기/쓰기 권한을 제공하는 path입니다.

예외

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

또는

path에 읽기 전용인 파일이 지정되었습니다.

또는

path는 숨겨진 파일을 지정했습니다.

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

path이(가) null인 경우

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

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

파일을 만드는 중 I/O 오류가 발생했습니다.

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

예제

다음 예제에서는 지정된 경로에 파일을 만들고, 파일에 일부 정보를 쓰고, 파일에서 읽습니다.

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

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Create the file, or overwrite if the file exists.
   FileStream^ fs = File::Create( path );
   try
   {
      array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is some text in the file." );
      
      // 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.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
            delete (IDisposable^)sr;
   }
}
using System;
using System.IO;
using System.Text;

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

        try
        {
            // Create the file, or overwrite if the file exists.
            using (FileStream fs = File.Create(path))
            {
                byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }

            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}
open System.IO
open System.Text

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

// Create the file, or overwrite if the file exists.
do
    use fs = File.Create path

    let info =
        UTF8Encoding(true)
            .GetBytes "This is some text in the file."
    // Add some information to the file.
    fs.Write(info, 0, info.Length)

// Open the stream and read it back.
do
    use sr = File.OpenText path
    let mutable s = sr.ReadLine()

    while isNull s |> not do
        printfn $"{s}"
        s <- sr.ReadLine()
Imports System.IO
Imports System.Text

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

    Try
      ' Create the file, or overwrite if the file exists.
      Using fs As FileStream = File.Create(path)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

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

      ' Open the stream and read it back. 
      Using sr As StreamReader = File.OpenText(path)
        Do While sr.Peek() >= 0
          Console.WriteLine(sr.ReadLine())
        Loop
      End Using

    Catch ex As Exception
      Console.WriteLine(ex.ToString())
    End Try

  End Sub
End Class

설명

이 메서드에서 만든 개체의 FileStream 기본 FileShareNone은 입니다. 원래 파일 핸들을 닫을 때까지 다른 프로세스 또는 코드는 만든 파일에 액세스할 수 없습니다.

이 메서드는 기본 버퍼 크기가 Create(String, Int32) 4,096바이트인 메서드 오버로드와 동일합니다.

path 매개 변수는 상대 경로 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 를 참조하세요 GetCurrentDirectory.

지정된 파일이 없으면 생성됩니다. 가 존재하고 읽기 전용이 아니면 콘텐츠가 삭제되고 덮어씁니다.

기본적으로 새 파일에 대한 전체 읽기/쓰기 액세스 권한이 모든 사용자에게 부여됩니다. 파일 읽기/쓰기 액세스를 사용 하 여 열고 닫아야 다른 애플리케이션에서 열 수 있습니다.

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

추가 정보

적용 대상

Create(String, Int32)

Source:
File.cs
Source:
File.cs
Source:
File.cs

버퍼 크기를 지정하여 지정된 경로의 파일을 만들거나 자르거나 덮어씁니다.

public:
 static System::IO::FileStream ^ Create(System::String ^ path, int bufferSize);
public static System.IO.FileStream Create (string path, int bufferSize);
static member Create : string * int -> System.IO.FileStream
Public Shared Function Create (path As String, bufferSize As Integer) As FileStream

매개 변수

path
String

만들 파일의 경로와 이름입니다.

bufferSize
Int32

파일 읽기 및 쓰기를 위해 버퍼링할 바이트 수입니다.

반환

FileStream에 지정된 파일에 대한 읽기/쓰기 권한을 제공하는 지정된 버퍼 크기의 path입니다.

예외

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

또는

path에 읽기 전용인 파일이 지정되었습니다.

또는

path는 숨겨진 파일을 지정했습니다.

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

path이(가) null인 경우

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

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

파일을 만드는 중 I/O 오류가 발생했습니다.

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

예제

다음 예제에서는 지정된 버퍼 크기의 파일을 만듭니다.

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

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Create the file, or overwrite if the file exists.
   FileStream^ fs = File::Create( path, 1024 );
   try
   {
      array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is some text in the file." );
      
      // 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.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
            delete (IDisposable^)sr;
   }
}
using System;
using System.IO;
using System.Text;

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

        // Create the file, or overwrite if the file exists.
        using (FileStream fs = File.Create(path, 1024))
        {
            byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
open System.IO
open System.Text

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

// Create the file, or overwrite if the file exists.
do
    use fs = File.Create(path, 1024)

    let info =
        UTF8Encoding(true)
            .GetBytes "This is some text in the file."
    // Add some information to the file.
    fs.Write(info, 0, info.Length)

// Open the stream and read it back.
do
    use sr = File.OpenText path
    let mutable s = sr.ReadLine()

    while isNull s |> not do
        printfn $"{s}"
        s <- sr.ReadLine()
Imports System.IO
Imports System.Text

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

    Try
      ' Create the file, or overwrite if the file exists.
      Using fs As FileStream = File.Create(path, 1024)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

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

      ' Open the stream and read it back. 
      Using sr As StreamReader = File.OpenText(path)
        Do While sr.Peek() >= 0
          Console.WriteLine(sr.ReadLine())
        Loop
      End Using

    Catch ex As Exception
      Console.WriteLine(ex.ToString())
    End Try

  End Sub
End Class

설명

이 메서드에서 만든 개체의 FileStream 기본 FileShareNone은 입니다. 원래 파일 핸들을 닫을 때까지 다른 프로세스 또는 코드는 만든 파일에 액세스할 수 없습니다.

path 매개 변수는 상대 경로 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 를 참조하세요 GetCurrentDirectory.

이 메서드는 생성자 오버로드와 FileStream(String, FileMode, FileAccess, FileShare, Int32) 동일합니다. 지정된 파일이 없으면 생성됩니다. 가 있고 읽기 전용이 아니면 내용이 바뀝니다.

기본적으로 새 파일에 대한 전체 읽기/쓰기 액세스 권한이 모든 사용자에게 부여됩니다. 파일 읽기/쓰기 액세스를 사용 하 여 열고 닫아야 다른 애플리케이션에서 열 수 있습니다.

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

추가 정보

적용 대상

Create(String, Int32, FileOptions)

Source:
File.cs
Source:
File.cs
Source:
File.cs

지정된 경로에 파일을 만들거나 덮어써서 버퍼 크기와 파일을 만들거나 덮어쓰는 방법을 설명하는 옵션을 지정합니다.

public:
 static System::IO::FileStream ^ Create(System::String ^ path, int bufferSize, System::IO::FileOptions options);
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options);
static member Create : string * int * System.IO.FileOptions -> System.IO.FileStream
Public Shared Function Create (path As String, bufferSize As Integer, options As FileOptions) As FileStream

매개 변수

path
String

만들 파일의 경로와 이름입니다.

bufferSize
Int32

파일 읽기 및 쓰기를 위해 버퍼링할 바이트 수입니다.

options
FileOptions

파일을 만들거나 덮어쓸 방식을 설명하는 FileOptions 값 중 하나입니다.

반환

지정된 버퍼 크기의 새 파일입니다.

예외

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

또는

path에 읽기 전용인 파일이 지정되었습니다.

또는

path는 숨겨진 파일을 지정했습니다.

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

path이(가) null인 경우

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

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

파일을 만드는 중 I/O 오류가 발생했습니다.

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

설명

path 매개 변수는 상대 경로 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 를 참조하세요 GetCurrentDirectory.

이 메서드는 생성자 오버로드와 FileStream(String, FileMode, FileAccess, FileShare, Int32) 동일합니다. 지정된 파일이 없으면 생성됩니다. 가 있고 읽기 전용이 아니면 내용이 바뀝니다.

기본적으로 새 파일에 대한 전체 읽기/쓰기 액세스 권한이 모든 사용자에게 부여됩니다. 파일 읽기/쓰기 액세스를 사용 하 여 열고 닫아야 다른 애플리케이션에서 열 수 있습니다.

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

적용 대상

Create(String, Int32, FileOptions, FileSecurity)

지정된 경로에 파일을 만들거나 덮어써서 버퍼 크기, 파일을 만들거나 덮어쓰는 방법을 설명하는 옵션 그리고 파일에 대한 액세스 제어 및 감사 보안을 결정하는 값을 지정합니다.

public:
 static System::IO::FileStream ^ Create(System::String ^ path, int bufferSize, System::IO::FileOptions options, System::Security::AccessControl::FileSecurity ^ fileSecurity);
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity fileSecurity);
static member Create : string * int * System.IO.FileOptions * System.Security.AccessControl.FileSecurity -> System.IO.FileStream
Public Shared Function Create (path As String, bufferSize As Integer, options As FileOptions, fileSecurity As FileSecurity) As FileStream

매개 변수

path
String

만들 파일의 경로와 이름입니다.

bufferSize
Int32

파일 읽기 및 쓰기를 위해 버퍼링할 바이트 수입니다.

options
FileOptions

파일을 만들거나 덮어쓸 방식을 설명하는 FileOptions 값 중 하나입니다.

fileSecurity
FileSecurity

파일의 액세스 제어 및 감사 보안을 결정하는 FileSecurity 개체입니다.

반환

지정된 버퍼 크기, 파일 옵션 및 파일 보안이 적용된 새 파일입니다.

예외

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

또는

path에 읽기 전용인 파일이 지정되었습니다.

또는

path는 숨겨진 파일을 지정했습니다.

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

path이(가) null인 경우

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

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

파일을 만드는 중 I/O 오류가 발생했습니다.

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

설명

매개 변수는 path 상대 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 를 참조하세요 GetCurrentDirectory.

이 메서드는 생성자 오버로드와 FileStream(String, FileMode, FileAccess, FileShare, Int32) 동일합니다. 지정된 파일이 없으면 만들어집니다. 가 있고 읽기 전용이 아니면 내용이 바뀝니다.

기본적으로 새 파일에 대한 전체 읽기/쓰기 액세스 권한은 모든 사용자에게 부여됩니다. 파일 읽기/쓰기 액세스를 사용 하 여 열고 닫아야 다른 애플리케이션에서 열 수 있습니다.

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

중요

이 메서드는 형식 Create(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)으로 .NET Core 3.1로 이식되었습니다.

적용 대상