File.Create 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建或截断并覆盖指定路径中的文件。
重载
| 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 中指定的文件的读/写访问。
例外
.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 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此方法创建的 对象的默认值None为 FileShare ;在原始文件句柄关闭之前,其他任何进程或代码都无法访问创建的文件。
此方法等效于 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 中指定的文件的读/写访问。
例外
.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 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此方法创建的 对象的默认值None为 FileShare ;在原始文件句柄关闭之前,其他任何进程或代码都无法访问创建的文件。
允许 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 值之一,它描述如何创建或覆盖该文件。
返回
具有指定缓冲区大小的新文件。
例外
.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 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 对象,用于确定文件的访问控制和审核安全性。
返回
具有指定的缓冲区大小、文件选项和文件安全性的新文件。
例外
.NET Framework 和 .NET Core 版本早于 2.1: path 是零长度字符串,仅包含空格,或包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。
              path 为 null。
指定的路径和/或文件名超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
创建文件时发生 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)。