Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a directory path was SUBST'd

How can I figure out if a file is in a folder that has been SUBST'ed or is located in a user folder using C#?

like image 995
petejamd Avatar asked Jun 10 '10 16:06

petejamd


People also ask

What does SUBST do in CMD?

Use the SUBST command to substitute a drive letter for a path in order to treat a virtual drive (a reserved area rather than an actual disk drive) as a physical drive. If you enter the SUBST command without options , the program will display the name of the current virtual drives that are in effect (if any).

How do I undo a SUBST drive?

If you map a drive with the NET command, use the NET command to remove it, similarly if you substitute a drive letter with SUBST then use the SUBST command to remove it.


3 Answers

I think you need to P/Invoke QueryDosDevice() for the drive letter. Subst drives will return a symbolic link, similar to \??\C:\blah. The \??\ prefix indicates it is substituted, the rest gives you the drive+directory.

like image 107
Hans Passant Avatar answered Sep 19 '22 12:09

Hans Passant


This is the code I use to get the information if a path is substed: (Some parts come from pinvoke)

using System.Runtime.InteropServices;

[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;
}
like image 45
marsh-wiggle Avatar answered Sep 21 '22 12:09

marsh-wiggle


I think you have a few choices --

Via System.Management classes: http://briancaos.wordpress.com/2009/03/05/get-local-path-from-unc-path/

Or

Via P/Invoking this MAPI function: ScUNCFromLocalPath http://msdn.microsoft.com/en-us/library/cc842520.aspx

like image 40
Bill Avatar answered Sep 18 '22 12:09

Bill