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
- 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文件的读/写访问权限的 Apath。
例外
低于 2.1 的 .NET Framework 和 .NET Core 版本: 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此方法创建的对象具有默认值FileShareNone;在原始文件句柄关闭之前,任何其他进程或代码都无法访问创建的文件。
此方法等效于 Create(String, Int32) 使用默认缓冲区大小为 4,096 字节的方法重载。
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
如果指定的文件不存在,则会创建它;如果它确实存在且不是只读的,则内容将被删除并覆盖。
默认情况下,向所有用户授予对新文件的完整读/写访问权限。 该文件以读/写访问权限打开,必须先关闭该文件,然后才能由另一个应用程序打开该文件。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
另请参阅
适用于
Create(String, Int32)
- Source:
- File.cs
- Source:
- File.cs
- 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
为读取和写入文件缓冲的字节数。
返回
具有指定缓冲区大小的 A FileStream ,它提供对在中指定的 path文件的读/写访问权限。
例外
低于 2.1 的 .NET Framework 和 .NET Core 版本: 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此方法创建的对象具有默认值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
- 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描述如何创建或覆盖文件的值之一。
返回
具有指定缓冲区大小的新文件。
例外
低于 2.1 的 .NET Framework 和 .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 对象,用于确定文件的访问控制和审核安全性。
返回
具有指定缓冲区大小、文件选项和文件安全性的新文件。
例外
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
指定的路径无效(例如,它位于未映射的驱动器上)。
创建文件时出现 I/O 错误。
path 格式无效。
注解
允许参数 path 指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
此方法等效于 FileStream(String, FileMode, FileAccess, FileShare, Int32) 构造函数重载。 如果指定的文件不存在,则会创建它;如果它确实存在且不是只读的,则替换内容。
默认情况下,向所有用户授予对新文件的完整读/写访问权限。 该文件以读/写访问权限打开,必须先关闭该文件,然后才能由另一个应用程序打开该文件。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
Important
此方法以以下形式移植到 .NET Core 3.1:Create(FileInfo, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)。