FileSystemInfo 类
为 FileInfo 和 DirectoryInfo 对象提供基类。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class FileSystemInfo
Inherits MarshalByRefObject
Implements ISerializable
用法
Dim instance As FileSystemInfo
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class FileSystemInfo : MarshalByRefObject, ISerializable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class FileSystemInfo abstract : public MarshalByRefObject, ISerializable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public abstract class FileSystemInfo extends MarshalByRefObject implements ISerializable
SerializableAttribute
ComVisibleAttribute(true)
public abstract class FileSystemInfo extends MarshalByRefObject implements ISerializable
备注
FileSystemInfo 类包含文件和目录操作所共有的方法。FileSystemInfo 对象可以表示文件或目录,从而可以作为 FileInfo 或 DirectoryInfo 对象的基础。当分析许多文件和目录时,请使用该基类。
第一次调用时,FileSystemInfo 调用 Refresh 并返回 API 上的缓存信息以获取属性等。以后调用时,必须调用 Refresh 以获取信息的最新副本。
一个派生类只有从 FileIOPermissionAccess 枚举获得 AllAccess 权限时才能从 FileSystemInfo 继承。
在接受路径的成员中,路径可以是指文件或仅是目录。指定路径也可以是相对路径或者服务器和共享名称的统一命名约定 (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 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
删除文件。 |
|
重命名或移动文件。 |
|
复制文件。 |
|
创建目录。 |
System.IO.DirectoryInfo |
创建子目录。 |
|
重命名或移动目录。 |
|
删除目录。 |
|
复制目录。 |
|
按大小对目录中的文件排序。 |
|
查看目录中的文件。 |
|
查看目录的子目录。 |
|
查看目录的所有子目录中的所有文件。 |
GetFileSystemInfos |
查看目录大小。 |
Directory |
获取文件大小。 |
|
获取文件属性。 |
|
设置文件属性。 |
|
确定文件是否存在。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
检索文件扩展名。 |
|
检索文件的完全限定路径。 |
|
检索路径中的文件名和扩展名。 |
|
更改文件扩展名。 |
示例
下面的示例演示如何循环浏览所有文件和目录,查询有关每一项的一些信息。
Imports System.IO
Module Module1
Sub Main()
' Loop through all the immediate subdirectories of C.
For Each entry As String In Directory.GetDirectories("C:\")
DisplayFileSystemInfoAttributes(New DirectoryInfo(entry))
Next
' Loop through all the files in C.
For Each entry As String In Directory.GetFiles("C:\")
DisplayFileSystemInfoAttributes(New FileInfo(entry))
Next
End Sub
Sub DisplayFileSystemInfoAttributes(ByVal fsi As IO.FileSystemInfo)
' Assume that this entry is a file.
Dim entryType As String = "File"
' Determine if this entry is really a directory.
If (fsi.Attributes And FileAttributes.Directory) <> 0 Then
entryType = "Directory"
End If
' Show this entry's type, name, and creation date.
Console.WriteLine("{0} entry {1} was created on {2:D}", _
entryType, fsi.FullName, fsi.CreationTime)
End Sub
End Module
' Output will vary based on contents of drive C.
'
' Directory entry C:\Documents and Settings was created on Tuesday, November 25, 2003
' Directory entry C:\Inetpub was created on Monday, January 12, 2004
' Directory entry C:\Program Files was created on Tuesday, November 25, 2003
' Directory entry C:\RECYCLER was created on Tuesday, November 25, 2003
' Directory entry C:\System Volume Information was created on Tuesday, November 2, 2003
' Directory entry C:\WINDOWS was created on Tuesday, November 25, 2003
' File entry C:\IO.SYS was created on Tuesday, November 25, 2003
' File entry C:\MSDOS.SYS was created on Tuesday, November 25, 2003
' File entry C:\pagefile.sys was created on Saturday, December 27, 2003
.NET Framework 安全性
- FileIOPermission 关联的枚举:Unrestricted 安全操作:继承要求 用于从该类继承的权限。
继承层次结构
System.Object
System.MarshalByRefObject
System.IO.FileSystemInfo
System.IO.DirectoryInfo
System.IO.FileInfo
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
FileSystemInfo 成员
System.IO 命名空间