File.Create 方法

定義

建立或截斷並覆寫指定路徑中的檔案。

多載

Create(String)

建立或截斷並覆寫指定路徑中的檔案。

Create(String, Int32)

建立或截斷並覆寫指定路徑中的檔案,並指定緩衝區大小。

Create(String, Int32, FileOptions)

建立或覆寫指定路徑中的檔案,並指定緩衝區大小以及描述如何建立或覆寫檔案的選項。

Create(String, Int32, FileOptions, FileSecurity)

建立或覆寫指定路徑中的檔案,並指定緩衝區大小、描述如何建立或覆寫檔案的選項,以及決定檔案存取控制與稽核安全性的值。

Create(String)

來源:
File.cs
來源:
File.cs
來源:
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 指定了隱藏的檔案。

.NET Framework 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

建立檔案時發生 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)

來源:
File.cs
來源:
File.cs
來源:
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 指定了隱藏的檔案。

.NET Framework 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

建立檔案時發生 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)

來源:
File.cs
來源:
File.cs
來源:
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 指定了隱藏的檔案。

.NET Framework 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

建立檔案時發生 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 指定了隱藏的檔案。

.NET Framework 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

建立檔案時發生 I/O 錯誤。

path 格式無效。

備註

允許 path 參數指定相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

這個方法相當於建 FileStream(String, FileMode, FileAccess, FileShare, Int32) 構函式多載。 如果指定的檔案不存在,則會建立它;如果存在且不是唯讀的,則會取代內容。

根據預設,會將新檔案的完整讀取/寫入存取權授與所有使用者。 檔案會以讀取/寫入存取權開啟,而且必須先關閉,才能由另一個應用程式開啟。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

重要

此方法已以下列形式移植到 .NET Core 3.1: Create(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

適用於