简体   繁体   中英

Check is windows drive exists

I'm going to write C# code that checks Is the C,D,E... (Windows disk drive) exists, or not? And finally find that which drive exists in client windows, to copy there my files.

I want to write the code similar to the following logic:

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
      }
   }
}

try this:

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

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

Edited (Check exist)

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

       }

You can use Directory.Exists() to check if directory exists.

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

You can learn about that from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM