取系统的硬盘分区的盘符,用API函数:GetDriveType
[DllImport("kernel32.dll", EntryPoint="GetDriveType")]
public static extern int GetDriveType (string nDrive);
[DllImport("kernel32.dll", EntryPoint="GetDriveType")]
public static extern int GetDriveType (string nDrive);
调用:
string [] dirs = Environment.GetLogicalDrives(); //取得所有的盘符
foreach(string dir in dirs)
{
?if ( GetDriveType(dir) == 3 ) //是硬盘
?{
??? //加到列表中
?}
}
判断文件夹是否是系统文件加或隐藏目录:
使用DirectoryInfo类的Attribute属性
DirectoryInfo [] subDirs = dir.GetDirectories(); //dir是DirectoryInfo 类的一个实例
foreach(DirectoryInfo subDir in subDirs)
{
?? if ( subDir.Attributes.ToString().IndexOf("Hidden") < 0 || subDir.Attributes.ToString().IndexOf("System") < 0 )
?{
??...
?}
}

博客介绍了使用C#进行的两项操作。一是调用Environment.GetLogicalDrives()方法取得所有盘符,并筛选出硬盘盘符;二是使用DirectoryInfo类的Attribute属性判断文件夹是否为系统文件夹或隐藏目录,通过检查属性字符串中是否包含'Hidden'或'System'来实现。
1653

被折叠的 条评论
为什么被折叠?



