检查Windows驱动器是否存在。

3

我将编写C#代码来检查C、D、E等(Windows磁盘驱动器)是否存在。最终找到客户端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
      }
   }
}

1
使用 DriveInfo.GetDrives - chaliasos
+1 @R.S 因为好问题总是被投票支持的。 - RajeshKdev
@RJK 谢谢。我觉得我的答案太业余了 :( - R.S
2个回答

7

试试这个:

   //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)
       {

       }

我也看到了你的编辑。谢谢,你的回答帮了我很多!但愿我有足够的声望来为你投票... :( - R.S
抱歉,此内容无法翻译,因为它似乎是一个随意编写的字符串。请提供准确的句子或描述以进行翻译。 - R.S

2
您可以使用Directory.Exists()来检查目录是否存在。
foreach (DriveInfo item in DriveInfo.GetDrives())
{
        if (Directory.Exists(item.Name))
        {
            // item.name is existed
        }
}

你可以从这里了解相关信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接