語言

File.Create 方法

定義

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

多載

名稱 Description
Create(String)

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

Create(String, Int32)

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

Create(String, Int32, FileOptions)

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

Create(String, Int32, FileOptions, FileSecurity)

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

Create(String)

來源:
File.cs
來源:
File.cs
來源:
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 A 提供對指定檔案path的讀寫存取。

例外狀況

來電者沒有所需的權限。

-或-

path 指定一個唯讀的檔案。

-或-

path 指定一個隱藏的檔案。

.NET Framework 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

path 是 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

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

path 格式無效。

範例

以下範例在指定路徑建立檔案,寫入部分資訊,並從檔案讀取。

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 物件預設 FileShare 值為 None;在原始檔案 handle 關閉前,其他程序或程式碼無法存取所建立的檔案。

此方法相當於 Create(String, Int32) 使用預設緩衝區大小 4,096 位元組的方法超載。

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

若指定的檔案不存在,則會建立該檔案;如果有該檔案且非唯讀,內容會被刪除並覆寫。

根據預設,對新檔案的完整讀取/寫入存取權會授與所有使用者。 檔案以讀寫權限開啟,必須先關閉後,其他應用程式才能開啟。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

另請參閱

適用於

Create(String, Int32)

來源:
File.cs
來源:
File.cs
來源:
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 A 具有指定的緩衝區大小,能提供對指定檔案的讀寫存取。path

例外狀況

來電者沒有所需的權限。

-或-

path 指定一個唯讀的檔案。

-或-

path 指定一個隱藏的檔案。

.NET Framework 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

path 是 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

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

path 格式無效。

範例

以下範例建立一個緩衝區大小的檔案。

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 物件預設 FileShare 值為 None;在原始檔案 handle 關閉前,其他程序或程式碼無法存取所建立的檔案。

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

此方法等同 FileStream(String, FileMode, FileAccess, FileShare, Int32) 於建構子過載。 若指定的檔案不存在,則會建立該檔案;如果有該內容且非唯讀,則會被替換。

根據預設,對新檔案的完整讀取/寫入存取權會授與所有使用者。 檔案以讀寫權限開啟,必須先關閉後,其他應用程式才能開啟。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

另請參閱

適用於

Create(String, Int32, FileOptions)

來源:
File.cs
來源:
File.cs
來源:
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 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

path 是 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

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

path 格式無效。

備註

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

此方法等同 FileStream(String, FileMode, FileAccess, FileShare, Int32) 於建構子過載。 若指定的檔案不存在,則會建立該檔案;如果有該內容且非唯讀,則會被替換。

根據預設,對新檔案的完整讀取/寫入存取權會授與所有使用者。 檔案以讀寫權限開啟,必須先關閉後,其他應用程式才能開啟。

關於常見 I/O 任務的清單,請參見 Common 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 與 .NET Core 版本 2.1 以前: path 為零長度字串、僅含空白,或包含一個或多個無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

path 是 null。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

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

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

path 格式無效。

備註

path參數允許指定相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory。

此方法等同 FileStream(String, FileMode, FileAccess, FileShare, Int32) 於建構子過載。 若指定的檔案不存在,則會建立該檔案;如果有該內容且非唯讀,則會被替換。

根據預設,對新檔案的完整讀取/寫入存取權會授與所有使用者。 檔案以讀寫權限開啟,必須先關閉後,其他應用程式才能開啟。

關於常見 I/O 任務的清單,請參見 Common I/O 任務。

Important

此方法後來以以下形式移植至 .NET Core 3.1:Create(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)。

適用於