gpt4 book ai didi

c# - 检查 Windows 驱动器是否存在

转载 作者:可可西里 更新时间:2023-11-01 14:42:51 25 4
gpt4 key购买 nike

我要编写 C# 代码来检查 C、D、E...(Windows 磁盘驱动器)是否存在?最后找到客户端窗口中存在哪个驱动器,将我的文件复制到那里。

我想写出类似如下逻辑的代码:

If ( !Exist(Drive "C:\" ) )
{
If ( !Exist(Drive "D:\" ) )
{
If ( !Exist(Drive "E:\" ) )
{
...
search to fined existence drive
copy file to a path of that existence drive
}
}
}

最佳答案

试试这个:

   //Get Drive names with DriveInfo.GetDrives()
var drives= DriveInfo.GetDrives();

foreach (var item in drives)
{
//Do Something
}

已编辑(检查是否存在)

   var drives= DriveInfo.GetDrives();
if (drives.Where(data => data.Name == "C:\\").Count() == 1 &&
drives.Where(data => data.Name == "D:\\").Count() == 1 &&
drives.Where(data => data.Name == "E:\\").Count() == 1)
{

}

关于c# - 检查 Windows 驱动器是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16118927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com