不允许在程序中自己构造DriveInfo的实例,可以通过DriveInfo的静态方法GetDrives()获得windows系统中所有的磁盘,包括硬盘,cd以及u盘;注意在访问磁盘属性时需要先判断其IsReady属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace aboutio
{
&nbs
p; class
PRogr
am {
static void M
ain(string[]
args)
{
DriveInfo[] drives = DriveInfo.GetDrives();
for
each (DriveInfo drive in drives)
{
if(drive.IsReady)
Console.Wr
ITeLine("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}",drive.DriveTy
PE, drive.VolumeLabel,drive.Name,drive.
totalSize,drive.TotalFreeSpace);
else
Console.WriteLine("类型:{0} is not ready",drive.DriveType);
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.T
hreading;
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 Erro
reventHandler(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. GetFileNameWithoutExtension(“d:\\abc.txt”) 将返回abc
5)Path. GetInvali
需要注意的是,以上提到的这几个文件系统相关的类的底层都调用了windows的api,也就是说这些类只可以在windows系统下用,而在其他操作系统下是不可用的。
@H_331_