.net 流氓
天下无难事,有志者成之;天下无易事,有恒者得之。

System.IO系列目录

1.Windows文件目录处理

2.Stream 以及 StreamReader和StreamWriter

3.压缩文件

4.共享内存,内存映射文件

5.使用管道在进程间通信

6.独立存储区

7.端口

在.Net中处理系统文件相关的几个类分别是File、Directory、FileInfo、DirectoryInfo、DriveInfo、FileSystemWatcher。本文介绍下这几个类的用法。

1.File类提供静态方法用来创建、移动、复制、删除文件的操作,并可以打开文件流

2.Directory类提供静态方法用来创建、移动、复制、删除目录的操作

3.FileInfo类用类实例实现创建、复制、移动、删除文件的操作

4.DirectoryInfo提供创建、移动、复制、删除目录的操作,并可以枚举子目录

5.DriveInfo可以获得windows操作系统中的磁盘信息

6.FileSystemWatcher用来监视文件或目录变化,并引发事件

7.Path类提供文件名目录名操作的静态方法

File、FileInfo、Directory、DirectoryInfo这几个类的使用方法都非常简单就不做赘述了。

1.如何使用DriveInfo获得windows系统磁盘信息

不允许在程序中自己构造DriveInfo的实例,可以通过DriveInfo的静态方法GetDrives()获得windows系统中所有的磁盘,包括硬盘,cd以及u盘;注意在访问磁盘属性时需要先判断其IsReady属性是否为true,IsReady为false时访问磁盘的一些属性时会抛出异常。如下实例代码:

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.IO;
06   
07 namespace aboutio
08 {
09     class Program
10     {
11         static void Main(string[] args)
12         {
13             DriveInfo[] drives = DriveInfo.GetDrives();
14   
15             foreach (DriveInfo drive in drives)
16             {
17                 if(drive.IsReady)
18                     Console.WriteLine("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}",drive.DriveType, drive.VolumeLabel,drive.Name,drive.TotalSize,drive.TotalFreeSpace);
19                 else
20                     Console.WriteLine("类型:{0}  is not ready",drive.DriveType);
21             }
22   
23             Console.ReadLine();
24         }
25     }
26 }

2. 使用FileSystemWatcher监视目录

FileSystemWatcher用来监视目录或者文件的修改,创建,删除,要使FileSystemWatcher开始监视必须设置其EnableRaisingEvents属性为true,如下示例:

01 using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.IO;
06 using System.Threading;
07   
08 namespace UseFileSystemWatcher
09 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //声明要监视的目录
15             string watchPath = "D:\\watch";
16             FileSystemWatcher watcher = new FileSystemWatcher(watchPath, "*.*");
17             //添加文件变化处理事件
18             watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
19             //添加文件创建处理事件
20             watcher.Created += new FileSystemEventHandler(Watcher_Created);
21             //添加文件删除处理事件
22             watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);            
23             //添加错误处理
24             watcher.Error += new ErrorEventHandler(Watcher_Error);
25             //启动监视
26             watcher.EnableRaisingEvents = true;
27             Thread.Sleep(1000 * 60);
28             Console.WriteLine("press any key to exit..");
29             Console.Read();
30         }
31   
32         static void Watcher_Error(object sender, ErrorEventArgs e)
33         {
34             Console.WriteLine("错误:" + e.ToString());
35         }
36   
37         static void Watcher_Deleted(object sender, FileSystemEventArgs e)
38         {
39             Console.WriteLine(e.ChangeType + ":" + e.FullPath);
40         }
41   
42         static void Watcher_Created(object sender, FileSystemEventArgs e)
43         {
44             Console.WriteLine(e.ChangeType + ":" + e.FullPath);
45         }
46   
47         static void Watcher_Changed(object sender, FileSystemEventArgs e)
48         {
49             Console.WriteLine(e.ChangeType + ":" + e.FullPath);
50         }
51     }
52 }

3. Path 类提供了一组处理路径的静态方法

1)Path.GetDirectoryName(string path) 返回目录名,需要注意路径末尾是否有反斜杠对结果是有影响的,如下:

Path.GetDirectoryName("d:\\abc") 将返回 d:\

Path.GetDirectoryName("d:\\abc\") 将返回 d:\abc

2)Path.GetRandomFileName()将返回随机的文件名

3)Path. GetFileNameWithoutExtension(“d:\\abc.txt”) 将返回abc

4)Path.GetInvalidPathChars() 将返回禁止在路径中使用的字符

5)Path. GetInvalidFileNameChars()将返回禁止在文件名中使用的字符

6)  Path.Combine(string left,string right)合并两个路径

需要注意的是,以上提到的这几个文件系统相关的类的底层都调用了windows的api,也就是说这些类只可以在windows系统下用,而在其他操作系统下是不可用的。

posted on 2011-08-04 19:49  .net 流氓  阅读(396)  评论(0编辑  收藏  举报