检查是否存在windows驱动器

问题描述:

我打算编写检查C,D,E ...(Windows磁盘驱动器)是否存在的C#代码?最后找到客户端窗口中存在哪个驱动器,以复制我的文件。检查是否存在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](http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx?cs-save-lang=1&cs-lang=vb#code- snippet-1) – Schaliasos 2013-04-20 09:55:19

+0

谢谢♥:)♥:)♥:)♥ – 2013-04-20 10:01:11

+0

+1 @RS BeCoz好问题总是在这里投票。 – RajeshKdev 2013-04-20 10:18:55

试试这个:

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

     } 
+0

谢谢亲爱的。这很有用。 – 2013-04-20 10:00:06

+0

我也看到你的编辑。谢谢,你的回答非常有帮助!我希望我有足够的声望投票给你... :( – 2013-04-20 10:09:09

+0

chakhlesim(chakker o mokhlesim):D – 2013-04-20 10:18:42

您可以使用Directory.Exists()检查目录是否存在。

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

你可以从here了解到。

+1

damet garm dash farhad♥(谢谢亲爱的Farhad) – 2013-04-20 10:11:03

+0

:))你是欢迎 – 2013-04-20 10:19:20