如何確定某個文件是否位於使用C#的SUBST'ed或位於用戶文件夾中的文件夾中?如何確定一個目錄路徑是否被隱藏
回答
我認爲你需要P/Invoke QueryDosDevice()作爲盤符。 Subst驅動器將返回一個符號鏈接,類似於\ ?? \ C:\ blah。 \ ?? \前綴表示它被替換,其餘的則爲您提供drive +目錄。
如果SUBST運行時沒有參數,它會生成所有當前替換的列表。獲取列表,並根據列表檢查您的目錄。
還有一個將卷映射到目錄的問題。我從來沒有試圖檢測到這些,但掛載點目錄顯示的不同於普通目錄,因此它們必須具有某種不同的屬性,並且可以檢測到這些屬性。
我覺得你有幾個選擇 -
通過System.Management類: http://briancaos.wordpress.com/2009/03/05/get-local-path-from-unc-path/
或者
通過P /調用此MAPI函數: ScUNCFromLocalPath http://msdn.microsoft.com/en-us/library/cc842520.aspx
這看起來很有希望,看着它 – petejamd 2010-06-10 17:36:16
這是我使用來獲取信息,如果路徑substed代碼: (部分來自pinvoke)
[DllImport("kernel32.dll", SetLastError=true)]
static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
public static bool IsSubstedPath(string path, out string realPath)
{
StringBuilder pathInformation = new StringBuilder(250);
string driveLetter = null;
uint winApiResult = 0;
realPath = null;
try
{
// Get the drive letter of the path
driveLetter = Path.GetPathRoot(path).Replace("\\", "");
}
catch (ArgumentException)
{
return false;
//<------------------
}
winApiResult = QueryDosDevice(driveLetter, pathInformation, 250);
if(winApiResult == 0)
{
int lastWinError = Marshal.GetLastWin32Error(); // here is the reason why it fails - not used at the moment!
return false;
//<-----------------
}
// If drive is substed, the result will be in the format of "\??\C:\RealPath\".
if (pathInformation.ToString().StartsWith("\\??\\"))
{
// Strip the \??\ prefix.
string realRoot = pathInformation.ToString().Remove(0, 4);
// add backshlash if not present
realRoot += pathInformation.ToString().EndsWith(@"\") ? "" : @"\";
//Combine the paths.
realPath = Path.Combine(realRoot, path.Replace(Path.GetPathRoot(path), ""));
return true;
//<--------------
}
realPath = path;
return false;
}
請確保你在你的班級 using System.Runtime.InteropServices; 否則你會得到錯誤。 – gg89 2016-09-23 05:11:06
- 1. 如何確定路徑是否在目錄中? (POSIX)
- 2. 如何測試一個目錄是否是python模塊路徑?
- 3. 確定NSString中的路徑是否是目錄或文件?
- 4. JQuery - 如何確定一個DOM元素是否「隱藏」?
- 5. GNU make,如何確定路徑是文件還是目錄?
- 6. 驗證是否路徑指向另一個路徑給定目錄
- 7. 如何創建一個隱藏目錄
- 8. 確定3D對象是否被另一3D對象隱藏
- 9. 如何確定路徑是vb6中的文件或目錄?
- 10. 如何確定一個目錄是否在同一分區
- 11. 如何檢查路徑是否指定卷根目錄
- 12. c#檢查一個目錄是否隱藏?
- 13. 確定兩個目錄名是否指向同一目錄
- 14. 檢查一個目錄是否存在遞歸反轉路徑
- 15. 確定一個點是否在Raphael.js中的路徑元素內
- 16. 如何測試目錄是否隱藏在C#中?
- 17. 類型名稱路徑是由一個靜態變量隱藏
- 18. AngularJs隱藏特定路徑/控制器
- 19. 如何確定一個目錄是否是一個有效的git目錄而不需要git?
- 20. 如何確定一個頁面是否被重定向
- 21. 確定一個點是否在路上
- 22. 確定路徑是否爲網絡路徑VBA
- 23. 確定ISV目錄路徑的最佳方法是什麼?
- 24. 如何檢查目錄是否在Bash的路徑上?
- 25. 如何確定鍵盤是否隱藏子視圖...?
- 26. 在服務器端,如何確定ModalPopupExtender目前是隱藏的?
- 27. Nessus是否依賴或使用目錄路徑來確定版本?
- 28. 是路徑目錄嗎?
- 29. 如何在Texlipse項目中隱藏文件夾「搜索路徑」
- 30. 如何隱藏.git項目目錄?
我不明白你所說的「subst'd」或「是什麼意思用戶文件夾「 – simendsjo 2010-06-10 16:13:43
'subst'是一個dos命令,它將爲一個目錄創建一個別名(例如,'subst T:C:\ workareas'將爲用戶文件夾創建一個指向C:\ workareas的新驅動器) ,想找出它是否在'C:\ Documents and Settings \%username%'乾淨。 – petejamd 2010-06-10 17:35:25