Check Drive Exists(string path)

前端 未结 8 979
半阙折子戏
半阙折子戏 2021-01-03 23:27

How to check the drive is exists in the system from the given string in WPF. I have tried the following

Ex: FileLocation.Text = \"K:\\TestDriv

8条回答
  •  耶瑟儿~
    2021-01-03 23:55

    string drive = Path.GetPathRoot(FileLocation.Text);   // e.g. K:\
    
    if (!Directory.Exists(drive))
    {
         MessageBox.Show("Drive " + drive + " not found or inaccessible", 
                         "Error", MessageBoxButton.OK);
         return;
    }
    

    Of course, additional sanity checks (does the path root have at least three characters, is the second one a colon) should be added, but this will be left as an exercise to the reader.

提交回复
热议问题