2010-06-10 100 views
5

如何確定某個文件是否位於使用C#的SUBST'ed或位於用戶文件夾中的文件夾中?如何確定一個目錄路徑是否被隱藏

+2

我不明白你所說的「subst'd」或「是什麼意思用戶文件夾「 – simendsjo 2010-06-10 16:13:43

+0

'subst'是一個dos命令,它將爲一個目錄創建一個別名(例如,'subst T:C:\ workareas'將爲用戶文件夾創建一個指向C:\ workareas的新驅動器) ,想找出它是否在'C:\ Documents and Settings \%username%'乾淨。 – petejamd 2010-06-10 17:35:25

回答

2

我認爲你需要P/Invoke QueryDosDevice()作爲盤符。 Subst驅動器將返回一個符號鏈接,類似於\ ?? \ C:\ blah。 \ ?? \前綴表示它被替換,其餘的則爲您提供drive +目錄。

2

如果SUBST運行時沒有參數,它會生成所有當前替換的列表。獲取列表,並根據列表檢查您的目錄。

還有一個將卷映射到目錄的問題。我從來沒有試圖檢測到這些,但掛載點目錄顯示的不同於普通目錄,因此它們必須具有某種不同的屬性,並且可以檢測到這些屬性。

0

這是我使用來獲取信息,如果路徑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; 
} 
+0

請確保你在你的班級 using System.Runtime.InteropServices; 否則你會得到錯誤。 – gg89 2016-09-23 05:11:06

相關問題