下面我写Function
检查是否存在File/Directory
路径,还有RecentPath
,它检索Function
检查过的最后一条路径。为什么函数输出错误的值?
private static String IRecentPath;
public static String RecentPath
{
get
{
return IRecentPath;
}
}
public static Boolean Exists(String Path, Int32 PathType = 0)
{
return Exist(Path, PathType);
}
internal static Boolean Exist(String Path, Int32 PathType = 0)
{
Boolean Report = false;
switch (PathType)
{
case 0:
Report = (Directory.Exists(Path) || File.Exists(Path));
IRecentPath = Path;
break;
case 1:
String MPath = AppDomain.CurrentDomain.BaseDirectory;
Report = (Directory.Exists(System.IO.Path.Combine(MPath, Path)) || File.Exists(System.IO.Path.Combine(MPath, Path)));
IRecentPath = System.IO.Path.Combine(MPath, Path);
break;
case 2:
String LPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Report = (Directory.Exists(System.IO.Path.Combine(LPath, Path)) || File.Exists(System.IO.Path.Combine(LPath, Path)));
IRecentPath = System.IO.Path.Combine(LPath, Path);
break;
default:
break;
}
return Report;
}
的问题是,RecentPath
总是检索,同时调用该函数,而不是最终的路径已设置的路径。
例子:
比方说,我需要检查,如果在myDocument
存在/user
目录,然后让已检查了最后的最近路径,因此:
Path.Exists("/user", 2);
MessageBox.Show(Path.RecentPath);
输出应该是C:\Users\Hossam\Documents\user\
但相反,它只是/user
。
喔上帝就是这样(Y)的感谢。 – Enumy 2014-09-21 21:46:00
“干扰”可能不是一个正确的词。这基本上是IO.Path.Combine的工作原理 - 它看到第二个参数以分隔符开始,显然假定它已经在根目录,并且只输出第二个路径。有关详细信息,请参见[MSDN](http://msdn.microsoft.com/zh-cn/library/fyy7a5kt%28v=vs.110%29.aspx)。 – Andrei 2014-09-21 21:46:16
@Andrei我知道它应该和这就是为什么它从来没有出现在我的脑海里,那是问题所在,但不知何故,当我只用'“用户”'它工作正常!!? – Enumy 2014-09-21 21:48:44