检查windows驱动器是否存在

本文关键字:存在 是否 驱动器 windows 检查 | 更新日期: 2023-09-27 18:10:11

我要写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
      }
   }
}

检查windows驱动器是否存在

try this:

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

您可以使用directory . exists()来检查目录是否存在。

foreach (DriveInfo item in DriveInfo.GetDrives())
{
        if (Directory.Exists(item.Name))
        {
            // item.name is existed
        }
}