FileInfo 类

来源:互联网 发布:巨杉数据库 福利 编辑:程序博客网 时间:2024/05/21 10:01

提供创建、复制、删除、移动和打开文件的实例方法,并且帮助创建 FileStream 对象。

System.Object

System.MarshalByRefObject

System.IO.FileSystemInfo

System.IO.FileInfo

[Visual Basic]

<Serializable>

NotInheritable Public Class FileInfo

Inherits FileSystemInfo

[C#]

[Serializable]

public sealed class FileInfo : FileSystemInfo

线程安全

此类型的所有公共静态(Visual Basic 中为 Shared)成员是线程安全的。但不保证任何实例成员是线程安全的。

备注

FileInfo 类用于典型的操作,如复制、移动、重命名、创建、打开、删除和追加到文件。许多 FileInfo 方法在您创建或打开文件时返回其他 I/O 类型。可以使用这些其他类型进一步操作文件。有关更多信息,请参见特定的 FileInfo 成员,如 OpenOpenReadOpenTextCreateText Create

如果打算多次重用某个对象,可考虑使用 FileInfo 的实例方法,而不是 File 类的相应静态方法,因为并不总是需要安全检查。

默认情况下,将向所有用户授予对新文件的完全读/写访问权限。

下表描述了用于自定义各种 FileInfo 方法的行为的枚举。

枚举 说明

FileAccess 指定对文件的读取和写入访问。

FileShare 为已在使用中的文件指定允许的访问级别。

FileMode 指定是保留还是改写现有文件的内容,并指定创建现有文件的请求是否会导致异常。

注意 在接受路径作为输入字符串的成员中,路径的格式必须正确,否则将引发异常。例如,如果路径是完全限定的但以空格开头,则路径在类的方法中不会被修剪。因此,路径的格式不正确,并将引发异常。同样,路径或路径的组合不能被完全限定两次。例如,“c:/temp c:/windows”在大多数情况下也将引发异常。在使用接受路径字符串的方法时,请确保路径的格式正确。

在接受路径的成员中,路径可以是指文件或仅是目录。指定路径也可以是相对路径或者服务器和共享名称的统一命名约定 (UNC) 路径。例如,以下都是可接受的路径:

C# 中的“c://MyDir//MyFile.txt”或 Visual Basic 中的“c:/MyDir/MyFile.txt”。

C# 中的“c://MyDir”或 Visual Basic 中的“c:/MyDir”。

C# 中的“MyDir//MySubdir”或 Visual Basic 中的“MyDir/MySubDir”。

C# 中的“////MyServer//MyShare”或 Visual Basic 中的“//MyServer/MyShare”。

有关使用此类的示例,请参见下面的“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作... 示例...

创建文本文件。 向文件写入文本

写入文本文件。 向文件写入文本

读取文本文件。 从文件读取文本

向文件中追加文本。 打开并附加到日志文件 File.AppendText FileInfo.AppendText

重命名或移动文件。 File.Move FileInfo.MoveTo

删除文件。 File.Delete FileInfo.Delete

复制文件。 File.Copy FileInfo.CopyTo

获取文件大小。 FileInfo.Length

获取文件属性。 File.GetAttributes

设置文件属性。 File.SetAttributes

确定文件是否存在。 File.Exists

读取二进制文件。 对刚创建的数据文件进行读取和写入

写入二进制文件。 对刚创建的数据文件进行读取和写入

检索文件扩展名。 Path.GetExtension

检索文件的完全限定路径。 Path.GetFullPath

检索路径中的文件名和扩展名。 Path.GetFileName

更改文件扩展名。 Path.ChangeExtension

.NET Framework 精简版平台说明: .NET Framework 精简版不支持目录属性的获取或设置操作。

下面的示例演示了 FileInfo 类的一些主要成员。

[Visual Basic]

Imports System

Imports System.IO

Public Class Test

Public Shared Sub Main()

Dim path As String = "c:/temp/MyTest.txt"

Dim path2 As String = path + "temp"

Dim fi As FileInfo = New FileInfo(path)

If fi.Exists = False Then

'Create a file to write to.

Dim sw As StreamWriter = fi.CreateText()

sw.WriteLine("Hello")

sw.WriteLine("And")

sw.WriteLine("Welcome")

sw.Flush()

sw.Close()

End If

Try

'Open the file to read from.

Dim sr As StreamReader = fi.OpenText()

Do While sr.Peek() >= 0

Console.WriteLine(sr.ReadLine())

Loop

sr.Close()

Dim fi2 As FileInfo = New FileInfo(path2)

'Ensure that the target does not exist.

fi2.Delete()

'Copy the file.

fi.CopyTo(path2)

Console.WriteLine("{0} was copied to {1}.", path, path2)

'Delete the newly created file.

fi2.Delete()

Console.WriteLine("{0} was successfully deleted.", path2)

Catch e As Exception

Console.WriteLine("The process failed: {0}.", e.ToString())

End Try

End Sub

End Class

[C#]

using System;

using System.IO;

class Test

{

public static void Main()

{

string path = @"c:/temp/MyTest.txt";

FileInfo fi1 = new FileInfo(path);

if (!fi1.Exists)

{

//Create a file to write to.

using (StreamWriter sw = fi1.CreateText())

{

sw.WriteLine("Hello");

sw.WriteLine("And");

sw.WriteLine("Welcome");

} }

//Open the file to read from.

using (StreamReader sr = fi1.OpenText())

{

string s = "";

while ((s = sr.ReadLine()) != null)

{

Console.WriteLine(s);

}

}

try

{

string path2 = path + "temp";

FileInfo fi2 = new FileInfo(path2);

//Ensure that the target does not exist.

fi2.Delete();

//Copy the file.

fi1.CopyTo(path2);

Console.WriteLine("{0} was copied to {1}.", path, path2);

//Delete the newly created file.

fi2.Delete();

Console.WriteLine("{0} was successfully deleted.", path2);

}

catch (Exception e)

{

Console.WriteLine("The process failed: {0}", e.ToString());

}

}

}

 
原创粉丝点击