Check Drive Exists(string path)

前端 未结 8 946
半阙折子戏
半阙折子戏 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:48

    You can use Environment.GetLogicalDrives() to obtain an string[] of logical drives in your system.

    var drive = Path.GetPathRoot(FileLocation.Text);
    if (Environment.GetLogicalDrives().Contains(drive, StringComparer.InvariantCultureIgnoreCase))
    {
             MessageBox.Show("Invalid Directory", "Error", MessageBoxButton.OK);
             return;
    }
    

提交回复
热议问题