检查驱动器存在(字符串路径) [英] Check Drive Exists(string path)

查看:118
本文介绍了检查驱动器存在(字符串路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查驱动器是存在于从WPF给定字符串的系统。我曾尝试以下



例如: FileLocation.Text =K:\TestDrive\XXX

 如果(!Directory.Exists(FileLocation.Text))
{
MessageBox.Show(无效目录,错误,MessageBoxButton.OK);
的回报;
}



据检查的完整路径,但则应检查 K:\从文本。能否请你指导我。



修改1: K:\TestDrive\XXX 也不是一成不变的



编辑2:我试过下面,在我的系统我遇到 3驱动器C,D和E 但它显示的

  Environment.SystemDirectory.Contains( D)的ToString()。 =FALSE


解决方案

 串驱动器= Path.GetPathRoot(FileLocation.Text); //例如K:\ 

如果
{
MessageBox.Show(驱动+驱动+找不到或无法访问,$ B(Directory.Exists(驱动器)!) $ b错误,MessageBoxButton.OK);
的回报;
}



当然,更多的完整性检查(做的根路径至少有三个字符是应添加的第二个的结肠),但是,这将作为练习留给读者


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:\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

EDIT 1: "K:\TestDrive\XXX" is not static

EDIT 2: I tried the below, in my system i'm having 3 drives C, D and E but it showing false.

Environment.SystemDirectory.Contains("D").ToString(); = "False"

解决方案

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.

这篇关于检查驱动器存在(字符串路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆