.NET文件与目录操作:从驱动器信息到流读写
立即解锁
发布时间: 2025-08-26 01:57:37 阅读量: 4 订阅数: 17 


精通VB 2008与.NET 3.5平台编程
### .NET 文件与目录操作:从驱动器信息到流读写
#### 1. 利用 DriveInfo 类获取驱动器信息
自 .NET 2.0 发布以来,`System.IO` 命名空间引入了 `DriveInfo` 类。与 `Directory.GetLogicalDrives()` 方法类似,`DriveInfo.GetDrives()` 静态方法可用于发现计算机驱动器的名称。不过,与 `Directory.GetLogicalDrives()` 不同的是,`DriveInfo` 能提供更详细的信息,如驱动器类型、可用空间、卷标等。
以下是示例代码:
```vb
Imports System.IO
Module Program
Sub Main()
Console.WriteLine("***** Fun with DriveInfo *****")
Dim myDrives As DriveInfo() = DriveInfo.GetDrives()
' Print stats about each drive.
For Each d As DriveInfo In myDrives
Console.WriteLine("******************************")
Console.WriteLine("-> Name: {0}", d.Name)
Console.WriteLine("-> Type: {0}", d.DriveType)
' Is the drive mounted?
If d.IsReady Then
Console.WriteLine("-> Free space: {0}", d.TotalFreeSpace)
Console.WriteLine("-> Format: {0}", d.DriveFormat)
Console.WriteLine("-> Label: {0}", d.VolumeLabel)
End If
Next
Console.ReadLine()
End Sub
End Module
```
该代码会遍历系统中的所有驱动器,并输出每个驱动器的相关信息。
#### 2. 使用 FileInfo 类操作文件
`FileInfo` 类可用于获取硬盘上现有文件的详细信息(如创建时间、大小、文件属性等),并辅助文件的创建、复制、移动和删除操作。除了从 `FileSystemInfo` 继承的功能外,`FileInfo` 类还有一些核心成员,如下表所示:
| 成员 | 含义 |
| ---- | ---- |
| `AppendText()` | 创建一个 `StreamWriter` 对象,用于向文件追加文本 |
| `CopyTo()` | 将现有文件复制到新文件 |
| `Create()` | 创建新文件并返回一个 `FileStream` 对象,用于与新创建的文件进行交互 |
| `CreateText()` | 创建一个 `StreamWriter` 对象,用于写入新的文本文件 |
| `Delete()` | 删除 `FileInfo` 实例所关联的文件 |
| `Directory` | 获取父目录的实例 |
| `DirectoryName` | 获取父目录的完整路径 |
| `Length` | 获取当前文件的大小 |
| `MoveTo()` | 将指定文件移动到新位置,并可指定新的文件名 |
| `Name` | 获取文件的名称 |
| `Open()` | 以各种读写和共享权限打开文件 |
| `OpenRead()` | 创建一个只读的 `FileStream` |
| `OpenText()` | 创建一个 `StreamReader` 对象,用于读取现有文本文件 |
| `OpenWrite()` | 创建一个只写的 `FileStream` 对象 |
以下是几种使用 `FileInfo` 类获取文件句柄的方法:
- **`FileInfo.Create()` 方法**:
```vb
Imports System.IO
Module Program
Sub Main()
' Make a new file on the C drive.
Dim f As New FileInfo("C:\Test.dat")
Dim fs As FileStream = f.Create()
' Use the FileStream object...
' Close down file stream.
fs.Close()
End Sub
End Module
```
该方法返回一个 `FileStream` 类型,它提供了对底层文件的同步和异步读写操作。由于 `FileStream` 实现了 `IDisposable` 接口,建议使用 `Using` 语句来确保资源的正确释放:
```vb
Sub Main()
' Defining a "Using scope" for file I/O
' types is ideal.
Dim f As New FileInfo("C:\Test.dat")
Using fs As FileStream = f.Create()
' Use the FileStream object...
End Using
End Sub
```
- **`FileInfo.Open()` 方法**:
```vb
Imports System.IO
Module Program
Sub Main()
' Make a new file via FileInfo.Open().
Dim f2 As New FileInfo("C:\Test2.dat")
Using fs2 As FileStream = f2.Open(FileMode.OpenOrCreate, _
FileAccess.ReadWrite, FileShare.None)
' Use the FileStream object...
End Using
End Sub
End Module
```
此重载的 `Open()` 方法需要三个参数:
- 第一个参数使用 `FileMode` 枚举指定 I/O 请求的类型,如下表所示:
| 成员 | 含义 |
| ---- | ---- |
| `CreateNew` | 通知操作系统创建新文件。如果文件已存在,则抛出 `IOException` |
| `Create` | 通知操作系统创建新文件。如果文件已存在,则覆盖它 |
| `Open` | 打开现有文件。如果文件不存在,则抛出 `FileNotFoundException` |
| `OpenOrCreate` | 如果文件存在则打开;否则创建新文件 |
| `Truncate` | 打开文件并将其大小截断为零字节 |
| `Append` | 打开文件,移动到文件末尾并开始写入操作(此标志只能与只写流一起使用)。如果文件不存在,则创建新文件 |
- 第二个参数是 `FileAccess` 枚举的值,用于确定底层流的读写行为:
| 成员 | 含义 |
| ---- | ---- |
| `Read` | 只读 |
| `Write` | 只写 |
| `ReadWrite` | 读写 |
- 第三个参数是 `FileShare` 枚举,指定文件在其他文件处理程序之间的共享方式:
| 成员 | 含义 |
| ---- | ---- |
| `None` | 不共享 |
| `Read` | 允许其他进程读取文件 |
| `Write` | 允许其他进程写入文件 |
| `ReadWrite` | 允许其他进程读写文件 |
- **`FileInfo.OpenRead()` 和 `FileInfo.OpenWrite()` 方法**:
```vb
Sub Main()
' Get a FileStream object with read-only permissions.
Dim f3 As New FileInfo("C:\Test3.dat")
Using readOnlyStream As FileStream = f3.OpenRead()
' Use FileStream...
End Using
' Get a FileStream object with write-only permissions.
Dim f4 As New FileInfo("C:\Test4.dat")
Using writeOnlyStream As FileStream = f4.OpenWrite()
' Use FileStream...
End Using
End Sub
```
这两个方法分别返回只读和只写的 `FileStream` 对象,无需提供各种枚举值。
- **`FileInfo.OpenText()` 方法**:
```vb
Sub Main()
' Get a StreamReader object.
Dim f5 As New FileInfo("C:\boot.ini")
Using sreader As Str
```
0
0
复制全文
相关推荐










