下面我寫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