2010-06-10 8 views
5

¿Cómo puedo saber si un archivo está en una carpeta que ha sido SUBST'ed o está ubicado en una carpeta de usuario usando C#?Cómo determinar si una ruta de directorio fue SUBST'd

+2

No entiendo qué quiere decir con "subst'd" o " carpeta de usuario " – simendsjo

+0

' subst' es un comando dos que creará un alias para un directorio (por ejemplo, 'subst T: C: \ workareas' creará una nueva unidad que apunta a C: \ workareas) para carpeta de usuario, i Estoy buscando averiguar si está en 'C: \ Documents and Settings \% username%' cleanly. – petejamd

Respuesta

2

Creo que necesitas P/Invocar QueryDosDevice() para la letra de la unidad. Las unidades Subst devolverán un enlace simbólico, similar a \ ?? \ C: \ blah. El prefijo \ ?? \ indica que está sustituido, el resto le da el directorio drive +.

2

Si SUBST se ejecuta sin parámetros, genera una lista de todas las sustituciones actuales. Obtenga la lista y verifique su directorio con la lista.

También está el problema de asignar un volumen a un directorio. Nunca intenté detectarlos, pero los directorios de punto de montaje aparecen de forma diferente que los directorios normales, por lo que deben tener un atributo diferente de algún tipo, y eso podría detectarse.

0

Este es el código que utilizo para obtener la información si se substed un camino: (Algunas partes provienen de 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

asegúrese de tener en su clase usando System.Runtime.InteropServices; De lo contrario, obtendrá un error. – gg89

Cuestiones relacionadas