2010-06-10 48 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

相关问题