当前位置: 移动技术网 > IT编程>开发语言>.net > .Net笔记:System.IO之windows文件操作的深入分析

.Net笔记:System.IO之windows文件操作的深入分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

龙岩个人二手房网,乐来影院,名著读后感2000字

在.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时访问磁盘的一些属性时会抛出异常。如下实例代码:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;

namespace aboutio
{
    class program
    {
        static void main(string[] args)
        {
            driveinfo[] drives = driveinfo.getdrives();

            foreach (driveinfo drive in drives)
            {
                if(drive.isready)
                    console.writeline("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}",drive.drivetype, drive.volumelabel,drive.name,drive.totalsize,drive.totalfreespace);
                else
                    console.writeline("类型:{0}  is not ready",drive.drivetype);
            }

            console.readline();
        }
    }
}

2. 使用filesystemwatcher监视目录
filesystemwatcher用来监视目录或者文件的修改,创建,删除,要使filesystemwatcher开始监视必须设置其enableraisingevents属性为true,如下示例:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;
using system.threading;

namespace usefilesystemwatcher
{
    class program
    {
        static void main(string[] args)
        {
            //声明要监视的目录
            string watchpath = "d:\\watch";
            filesystemwatcher watcher = new filesystemwatcher(watchpath, "*.*");
            //添加文件变化处理事件
            watcher.changed += new filesystemeventhandler(watcher_changed);
            //添加文件创建处理事件
            watcher.created += new filesystemeventhandler(watcher_created);
            //添加文件删除处理事件
            watcher.deleted += new filesystemeventhandler(watcher_deleted);          
            //添加错误处理
            watcher.error += new erroreventhandler(watcher_error);
            //启动监视
            watcher.enableraisingevents = true;
            thread.sleep(1000 * 60);
            console.writeline("press any key to exit..");
            console.read();
        }

        static void watcher_error(object sender, erroreventargs e)
        {
            console.writeline("错误:" + e.tostring());
        }

        static void watcher_deleted(object sender, filesystemeventargs e)
        {
            console.writeline(e.changetype + ":" + e.fullpath);
        }

        static void watcher_created(object sender, filesystemeventargs e)
        {
            console.writeline(e.changetype + ":" + e.fullpath);
        }

        static void watcher_changed(object sender, filesystemeventargs e)
        {
            console.writeline(e.changetype + ":" + e.fullpath);
        }
    }
}

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系统下用,而在其他操作系统下是不可用的。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网