在我的程序中,我需要檢查文件的多個路徑位於系統臨時文件文件夾內(例如C:\ Users \ Roman \ AppData \ Local \ Temp)。 我還沒有找到任何方法System.IO.File,System.IO.Directory和System.IO.Path這樣做的類。所以我創造了我自己的:是否有任何簡單的方法來檢查文件的路徑是否在某個文件夾(.NET框架)內?
public static bool IsSafeToDeleteFileOrDirectory(string path)
{
try
{
string tempPath = Path.GetFullPath(
Path.Combine(Path.GetTempPath(), ".\\")
);
string fullPath = Path.GetFullPath(path);
return fullPath.StartsWith(tempPath) &&
fullPath.Length > tempPath.Length;
}
catch (Exception ex)
{}
return false;
}
但我不確定它是否會一直工作。 除了遍歷文件夾樹並檢查每個子文件夾是否存在,其父文件夾是否爲TEMP,還有其他簡單解決方案嗎?
他已經這麼做了。 GetFullPath() – 2009-02-19 12:19:50