我在制作一个类似 PPT 的工具,这个工具有超链接模块,我需要关注的是超链接文件是否链接到 U 盘上了已给出提示。防止一些用户链接到自己电脑上,然后换个电脑又找不到
通过 dotnet 自带的 DriveInfo 类就能够很好地实现这一点。只需判断 DriveType 属性是否为 Removable 即可了解是否是 U 盘。判断原理是判断可插拔的盘
遍历当前系统的所有磁盘,判断有哪些是 U 盘的方法如下
foreach (var driveInfo in DriveInfo.GetDrives())
{
if (driveInfo.DriveType == DriveType.Removable)
{
Console.WriteLine($"{driveInfo.RootDirectory} 是 U 盘");
}
}点开 DriveType 还可以看到更多有趣的类型,比如光盘等
判断传入路径是否在 U 盘里,可用如下判断
var path = @"H:\lindexi\test.txt";
var isUDiskPath = IsUDiskPath(path);
Console.WriteLine($"Path={path} 是 U 盘={isUDiskPath}");
static bool IsUDiskPath(string path)
{
if (!Path.IsPathFullyQualified(path))
{
throw new ArgumentException($"路径必须是绝对路径。 Path={path}", nameof(path));
}
var pathRoot = Path.GetPathRoot(path);
if (pathRoot is null)
{
return false;
}
var driveInfo = new DriveInfo(pathRoot);
return driveInfo.DriveType == DriveType.Removable;
}软软对 DriveInfo 的容错设计是非常好的,接受的输入非常多:
H:\lindexi\test.txt 也是可以的H:\HY: 盘。此时不会抛出异常,只会让 DriveType 为 NoRootDirectory 类型本文的核心代码如下
var path = @"H:\lindexi\test.txt";
var isUDiskPath = IsUDiskPath(path);
Console.WriteLine($"Path={path} 是 U 盘={isUDiskPath}");
foreach (var driveInfo in DriveInfo.GetDrives())
{
if (driveInfo.DriveType == DriveType.Removable)
{
Console.WriteLine($"{driveInfo.RootDirectory} 是 U 盘");
}
}
static bool IsUDiskPath(string path)
{
if (!Path.IsPathFullyQualified(path))
{
throw new ArgumentException($"路径必须是绝对路径。 Path={path}", nameof(path));
}
var pathRoot = Path.GetPathRoot(path);
if (pathRoot is null)
{
return false;
}
var driveInfo = new DriveInfo(pathRoot);
return driveInfo.DriveType == DriveType.Removable;
}本文代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快
先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin daea5a766abb0f786c58137cd0066cb7bffb37e5以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin daea5a766abb0f786c58137cd0066cb7bffb37e5获取代码之后,进入 Workbench/CawrelibairquJojaijurhewe 文件夹,即可获取到源代码
更多技术博客,请参阅 博客导航