如何确定目录路径是否为SUBST

如何确定文件是否在SUBST的文件夹中或使用C#位于用户文件夹中?

我认为您需要P / Invoke QueryDosDevice()作为驱动器号。 Subst驱动器将返回一个符号链接,类似于\ ?? \ C:\ blah。 \ ?? \前缀表示它被替换,其余的给你驱动器+目录。

我想你有几个选择 –

通过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

如果在没有参数的情况下运行SUBST,则会生成所有当前替换的列表。 获取列表,并根据列表检查您的目录。

还存在将卷映射到目录的问题。 我从未尝试过检测这些,但是挂载点目录确实与常规目录不同,因此它们必须具有某种不同的属性,并且可以检测到。

这是我用来获取路径的信息的代码:(某些部分来自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; }