2016-06-20 63 views
1

我想检查UNC路径文件夹(从网络用户的输入)的存在,这里是我的代码:检查是否URL UNC目录路径存在

Directory.Exists("file://localhost/c$/folderName/"); //this always return false 

这是不重复的:how-to-quickly-check-if-unc-path-is-available因为我是处理url unc路径(使用“//”反斜杠)。

+0

*'我已经找到solutuion:'*,那么你可以张贴一个答案,而不是 –

+0

我已经加入的答案,但你绝不能编辑原创问题包括答案... – RhysO

回答

2

您需要使用URI类型。首先,你需要只是测试它使用

Directory.Exists(foo.LocalPath);使用UNC路径

Uri foo = new Uri("file://localhost/c$/folderName/");

定义一个新的URI。

这将返回一个布尔值,并允许您根据该值执行代码。

所以,你的整个代码会像下面:

Uri foo = new Uri("file://localhost/c$/folderName/"); 

if (!Directory.Exists(foo.LocalPath)) 
{ 
    Debug.Log("UNC does not exist or is not accessible!"); 
} 
else 
{ 
    Debug.Log("UNC exists!"); 
}