C# 检查驱动器是否存在(字符串路径)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17335072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check Drive Exists(string path)
提问by Ponmalar
How to check the drive is exists in the system from the given string in WPF. I have tried the following
如何从 WPF 中的给定字符串检查系统中是否存在驱动器。我已经尝试了以下
Ex:FileLocation.Text = "K:\TestDrive\XXX";
前任:FileLocation.Text = "K:\TestDrive\XXX";
if (!Directory.Exists(FileLocation.Text))
{
MessageBox.Show("Invalid Directory", "Error", MessageBoxButton.OK);
return;
}
It is checking full path but it should check "K:\" from the text. Could you please guide me
它正在检查完整路径,但是it should check "K:\" from the text. 你能指导我吗
EDIT 1:"K:\TestDrive\XXX" is not static
编辑 1:“ K:\TestDrive\XXX”不是静态的
EDIT 2:I tried the below, in my system i'm having 3 drives C, D and Ebut it showing false.
编辑 2:我尝试了以下操作,在我的系统中我有3 drives C, D and E但它显示false。
Environment.SystemDirectory.Contains("D").ToString(); = "False"
采纳答案by Heinzi
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.
当然,应该添加额外的完整性检查(路径根是否至少有三个字符,第二个是冒号),但这将作为练习留给读者。
回答by Rajeev Kumar
You can check drives in C# like this
您可以像这样在 C# 中检查驱动器
foreach (var drive in DriveInfo.GetDrives())
{
//Code goes here
}
回答by DreamChild
you can do follow
你可以关注
bool isDriveExists(string driveLetterWithColonAndSlash)
{
return DriveInfo.GetDrives().Any(x => x.Name == driveLetterWithColonAndSlash);
}
回答by Shujaat Abdi
you can try this....
你可以试试这个....
MessageBox.Show(Environment.SystemDirectory.Contains("D").ToString());
回答by Shujaat Abdi
This is Because Environment.SystemDirectory.XXXXX is all about where the system/windows is installed ...... not for whole HD.
这是因为 Environment.SystemDirectory.XXXXX 是关于系统/windows 的安装位置......而不是全高清。
for this you can use.....
为此,您可以使用.....
foreach (var item in System.IO.DriveInfo.GetDrives())
{
MessageBox.Show(item.ToString());
}
it will show all drives including USBs that are attached.....
它将显示所有驱动器,包括连接的 USB.....
回答by Pedro
You can use Environment.GetLogicalDrives()to obtain an string[]of logical drives in your system.
您可以使用Environment.GetLogicalDrives()获取string[]系统中的逻辑驱动器。
var drive = Path.GetPathRoot(FileLocation.Text);
if (Environment.GetLogicalDrives().Contains(drive, StringComparer.InvariantCultureIgnoreCase))
{
MessageBox.Show("Invalid Directory", "Error", MessageBoxButton.OK);
return;
}
回答by Pedro
I suppose this depends on what exactly you are hoping to accomplish. If you are trying to iterate through the drives and test to make sure each drive exists, then Environment.GetLogicalDrives()or DriveInfo.GetDrives()is appropriate as it allows you to iterate through the drives.
我想这取决于您究竟希望完成什么。如果您尝试遍历驱动器并进行测试以确保每个驱动器都存在,那么Environment.GetLogicalDrives()orDriveInfo.GetDrives()是合适的,因为它允许您遍历驱动器。
However, if all you care about is testing to see if ONE drive exists for a particular path, getting the entire list of drives to check if it is contained is a bit backwards. You would want to use Directory.Exists()as it just checks if that single path exists.
但是,如果您关心的只是测试某个特定路径是否存在一个驱动器,那么获取整个驱动器列表以检查它是否被包含就有点倒退了。您可能想要使用Directory.Exists()它,因为它只是检查该单个路径是否存在。
bool DriveExists(string fileLocation) {
string drive = Path.GetPathRoot(fileLocation); // To ensure we are just testing the root directory.
return Directory.Exists(drive); // Does the path exist?
}
回答by facepalm42
This will get if any drives' letter is E. You can change it to any other letter.
如果任何驱动器的字母是 E,这将得到。您可以将其更改为任何其他字母。
DriveInfo.GetDrives().Any(d => d.Name.ToUpper()[0] == 'E');

