簡體   English   中英

檢查是否存在Windows驅動器

[英]Check is windows drive exists

我要編寫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)
       {

       }

您可以使用Directory.Exists()來檢查目錄是否存在。

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

你可以從這里了解到這一點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM