2010-06-10 5 views
5

Comment savoir si un fichier se trouve dans un dossier SUBST'ed ou s'il se trouve dans un dossier utilisateur en utilisant C#?Comment déterminer si un chemin de répertoire était SUBST?

+2

Je ne comprends pas ce que vous entendez par « subst'd » ou " dossier utilisateur " – simendsjo

+0

' subst' est une commande DOS qui va créer un alias pour un répertoire (par exemple, 'T: C: \ workareas' va créer un nouveau lecteur qui pointe vers C: \ workareas) pour le dossier utilisateur, i cherche à savoir si c'est dans le 'C: \ Documents and Settings \% username%' proprement. – petejamd

Répondre

2

Je pense que vous devez P/Invoke QueryDosDevice() pour la lettre de lecteur. Les lecteurs Subst renvoient un lien symbolique, similaire à \ ?? \ C: \ blah. Le préfixe \ ?? \ indique qu'il est substitué, le reste vous donne le répertoire drive +.

2

Si SUBST est exécuté sans paramètres, il produit une liste de toutes les substitutions courantes. Obtenez la liste, et vérifiez votre répertoire par rapport à la liste.

Il y a aussi le problème de mapper un volume à un répertoire. Je n'ai jamais essayé de les détecter, mais les répertoires des points de montage apparaissent différemment des répertoires normaux. Ils doivent donc avoir un attribut différent, qui peut être détecté.

0

C'est le code que j'utilise pour obtenir l'information si un chemin est substed: (Certaines parties proviennent 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

être sûr que vous avez dans votre classe en utilisant System.Runtime.InteropServices; Sinon, vous obtiendrez une erreur. – gg89

Questions connexes