VB.Net - Windows文件系统
DirectoryInfo类
DirectoryInfo类是从FileSystemInfo类派生的。 它有各种方法来创建,移动和浏览目录和子目录。 此类不能继承。以下是DirectoryInfo类的一些常用属性:
S.N | 属性名称和说明 |
---|---|
1 | Attributes Gets the attributes for the current file or directory. 获取当前文件或目录的属性。 |
2 | CreationTime Gets the creation time of the current file or directory. 获取当前文件或目录的创建时间。 |
3 | Exists Gets a Boolean value indicating whether the directory exists. 获取指示该目录是否存在的布尔值。 |
4 | Extension Gets the string representing the file extension. 获取表示文件扩展名的字符串。 |
5 | FullName Gets the full path of the directory or file. 获取目录或文件的完整路径。 |
6 | LastAccessTime Gets the time the current file or directory was last accessed. 获取上次访问当前文件或目录的时间。 |
7 | Name Gets the name of this DirectoryInfo instance. 获取此DirectoryInfo实例的名称。 |
以下是DirectoryInfo类的一些常用方法:
S.N | 方法名称和用途 |
---|---|
1 | Public Sub Create Creates a directory. 创建目录。 |
2 | Public Function CreateSubdirectory (path As String ) As DirectoryInfo Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class. 在指定的路径上创建子目录或子目录。 指定的路径可以相对于DirectoryInfo类的此实例。 |
3 | Public Overrides Sub Delete Deletes this DirectoryInfo if it is empty. 如果DirectoryInfo为空,则删除它。 |
4 | Public Function GetDirectories As DirectoryInfo() Returns the subdirectories of the current directory. 返回当前目录的子目录。 |
5 | Public Function GetFiles As FileInfo() Returns a file list from the current directory. 从当前目录返回文件列表。 |
FileInfo类
FileInfo类是从FileSystemInfo类派生的。 它具有用于创建,复制,删除,移动和打开文件的属性和实例方法,并且有助于创建FileStream对象。 此类不能继承。以下是FileInfo类的一些常用属性:
S.N | 属性名称和说明 |
---|---|
1 | Attributes Gets the attributes for the current file. 获取当前文件的属性。 |
2 | CreationTime Gets the creation time of the current file. 获取当前文件的创建时间。 |
3 | Directory Gets an instance of the directory, which the file belongs to. 获取该文件所属的目录的实例。 |
4 | Exists Gets a Boolean value indicating whether the file exists. 获取指示文件是否存在的布尔值。 |
5 | Extension Gets the string representing the file extension. 获取表示文件扩展名的字符串。 |
6 | FullName Gets the full path of the file. 获取文件的完整路径。 |
7 | LastAccessTime Gets the time the current file was last accessed. 获取当前文件上次访问的时间。 |
8 | LastWriteTime Gets the time of the last written activity of the file. 获取文件的上次写入活动的时间。 |
9 | Length Gets the size, in bytes, of the current file. 获取当前文件的大小(以字节为单位)。 |
10 | Name Gets the name of the file. 获取文件的名称。 |
以下是FileInfo类的一些常用方法:
S.N | 方法名称和用途 |
---|---|
1 | Public Function AppendText As StreamWriter Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo. 创建一个StreamWriter,将文本附加到由FileInfo的实例表示的文件。 |
2 | Public Function Create As FileStream Creates a file. 创建文件。 |
3 | Public Overrides Sub Delete Deletes a file permanently. 永久删除文件。 |
4 | Public Sub MoveTo (destFileName As String ) Moves a specified file to a new location, providing the option to specify a new file name. 将指定的文件移动到新位置,提供指定新文件名的选项。 |
5 | Public Function Open (mode As FileMode) As FileStream Opens a file in the specified mode. 以指定模式打开文件。 |
6 | Public Function Open (mode As FileMode, access As FileAccess ) As FileStream Opens a file in the specified mode with read, write, or read/write access. 以指定模式打开具有读取,写入或读/写访问权限的文件。 |
7 | Public Function Open (mode As FileMode, access As FileAccess, share As FileShare ) As FileStream Opens a file in the specified mode with read, write, or read/write access and the specified sharing option. 以指定模式打开具有读取,写入或读/写访问权限以及指定的共享选项的文件。 |
8 | Public Function OpenRead As FileStream Creates a read-only FileStream 创建只读FileStream |
9 | Public Function OpenWrite As FileStream Creates a write-only FileStream. 创建只写FileStream。 |
示例:
以下示例演示了上述类的使用:Imports System.IO
Module fileProg
Sub Main()
'creating a DirectoryInfo object
Dim mydir As DirectoryInfo = New DirectoryInfo("c:\Windows")
' getting the files in the directory, their names and size
Dim f As FileInfo() = mydir.GetFiles()
Dim file As FileInfo
For Each file In f
Console.WriteLine("File Name: {0} Size: {1} ", file.Name, file.Length)
Next file
Console.ReadKey()
End Sub
End Module
当编译和运行程序时,它在Windows目录中显示文件名及其大小。
更多建议: