File 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用于创建、复制、删除、移动和打开单一文件的静态方法,并协助创建 FileStream 对象。
public ref class File abstract sealed
public ref class File sealed
public static class File
public sealed class File
[System.Runtime.InteropServices.ComVisible(true)]
public static class File
type File = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type File = class
Public Class File
Public NotInheritable Class File
- 继承
-
File
- 属性
示例
以下示例演示如何使用 File 类检查文件是否存在,并根据结果创建新文件并向其写入,或打开现有文件并从中读取。 在运行代码之前,请创建一个 c:\temp
文件夹。
using namespace System;
using namespace System::IO;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
if ( !File::Exists( path ) )
{
// Create a file to write to.
StreamWriter^ sw = File::CreateText( path );
try
{
sw->WriteLine( "Hello" );
sw->WriteLine( "And" );
sw->WriteLine( "Welcome" );
}
finally
{
if ( sw )
delete (IDisposable^)(sw);
}
}
// Open the file to read from.
StreamReader^ sr = File::OpenText( path );
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)(sr);
}
try
{
String^ path2 = String::Concat( path, "temp" );
// Ensure that the target does not exist.
File::Delete( path2 );
// Copy the file.
File::Copy( path, path2 );
Console::WriteLine( "{0} was copied to {1}.", path, path2 );
// Delete the newly created file.
File::Delete( path2 );
Console::WriteLine( "{0} was successfully deleted.", path2 );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
open System.IO
let path = @"c:\temp\MyTest.txt"
if File.Exists path |> not then
// Create a file to write to.
use sw = File.CreateText path
sw.WriteLine "Hello"
sw.WriteLine "And"
sw.WriteLine "Welcome"
// Open the file to read from.
do
use sr = File.OpenText path
let mutable s = sr.ReadLine()
while isNull s |> not do
printfn $"{s}"
s <- sr.ReadLine()
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
If File.Exists(path) = False Then
' Create a file to write to.
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
End Using
End If
' Open the file to read from.
Using sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
End Using
End Sub
End Class
注解
File使用 类执行典型的操作,例如复制、移动、重命名、创建、打开、删除和追加到单个文件一次。 还可以使用 File 类获取和设置与创建、访问和写入文件相关的文件属性或 DateTime 信息。 如果要对多个文件执行操作,请参阅 Directory.GetFiles 或 DirectoryInfo.GetFiles。
创建或打开文件时, File 许多方法返回其他 I/O 类型。 可以使用这些其他类型进一步操作文件。 有关详细信息,请参阅特定 File 成员,例如 OpenText、 CreateText或 Create。
由于所有方法都是 File 静态的,因此,如果只想执行一个 File 操作,则使用方法(而不是相应的 FileInfo 实例方法)可能更有效。 所有方法都需要 File 要操作的文件的路径。
类的 File 静态方法对所有方法执行安全检查。 如果要多次重用对象,请考虑改用 对应的实例方法 FileInfo ,因为安全检查并非始终是必需的。
默认情况下,向所有用户授予对新文件的完整读/写访问权限。
下表描述了用于自定义各种 File 方法行为的枚举。
枚举 | 描述 |
---|---|
FileAccess | 指定对文件的读取和写入访问权限。 |
FileShare | 指定已使用的文件允许的访问级别。 |
FileMode | 指定是保留还是覆盖现有文件的内容,以及创建现有文件的请求是否会导致异常。 |
注意
在接受路径作为输入字符串的成员中,该路径的格式必须正常,否则将引发异常。 例如,如果路径是完全限定的,但以空格开头,则不会在 类的方法中剪裁该路径。 因此,路径格式不正确,并引发异常。 同样,路径或路径组合不能完全限定两次。 例如,“c:\temp c:\windows”在大多数情况下也会引发异常。 使用接受路径字符串的方法时,请确保路径格式良好。
在接受路径的成员中,路径可以引用文件或仅引用目录。 指定的路径还可以引用服务器和共享名称的相对路径或通用命名约定 (UNC) 路径。 例如,以下所有路径都是可接受的路径:
"c:\\\MyDir\\\MyFile.txt"
在 C# 或"c:\MyDir\MyFile.txt"
Visual Basic 中。"c:\\\MyDir"
在 C# 或"c:\MyDir"
Visual Basic 中。"MyDir\\\MySubdir"
在 C# 或"MyDir\MySubDir"
Visual Basic 中。"\\\\\\\MyServer\\\MyShare"
在 C# 或"\\\MyServer\MyShare"
Visual Basic 中。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。