One of the devs on the BCL team created this cool little sample while investigation a Whidbey bug and I thought I’d share it with you…

 

C:\code >mount a: c:\MountPoint

Managed Unix-style mount program

Mounting volume a:\ at c:\MountPoint\

 

C:\code >dir c:\MountPoint

 Volume in drive C is Brian

 Volume Serial Number is 186C-51E7

 

 Directory of c:\MountPoint

 

01/27/2003  03:11 AM            15,229 Queue.cs

01/27/2003  06:10 PM            44,098 SortedDictionary.cs

01/27/2003  03:26 AM            11,100 Stack.cs

               3 File(s)         70,427 bytes

           

Here is the code…  Works fine for me on my Everett windows XP machine…  

 

using System;

using System.Runtime.InteropServices;

using System.Text;

using System.IO;

using System.ComponentModel;

 

public class UnixLikeMountProgram

{

    [DllImport("kernel32", CharSet=CharSet.Auto, BestFitMapping=false, SetLastError=true)]

    private static extern bool GetVolumeNameForVolumeMountPoint(String volumeName, StringBuilder uniqueVolumeName, int uniqueNameBufferCapacity);

 

    // unique volume name must be "\\?\Volume{GUID}\"

    [DllImport("kernel32", CharSet=CharSet.Auto, BestFitMapping=false, SetLastError=true)]

    private static extern bool SetVolumeMountPoint(String mountPoint, String uniqueVolumeName);

 

    [DllImport("kernel32", CharSet=CharSet.Auto, BestFitMapping=false, SetLastError=true)]

    private static extern bool DeleteVolumeMountPoint(String mountPoint);

 

 

    private static void Usage()

    {

        Console.WriteLine();

        Console.WriteLine("mount <volume name> <mount point>");

        Console.WriteLine("mount -u <mount point>");

    }

 

    private static void Mount(string volumeName, string mountPoint)

    {

        Console.WriteLine("Mounting volume {0} at {1}", volumeName, mountPoint);

        bool r;

        StringBuilder sb = new StringBuilder(1024);

        r = GetVolumeNameForVolumeMountPoint(volumeName, sb, sb.Capacity);

        if (!r)

            throw new Win32Exception(Marshal.GetLastWin32Error());

 

        String uniqueName = sb.ToString();

        r = SetVolumeMountPoint(mountPoint, uniqueName);

        if (!r)

            throw new Win32Exception(Marshal.GetLastWin32Error());

    }

 

    private static void Unmount(string mountPoint)

    {

        Console.WriteLine("Unmounting the volume at {0}", mountPoint);

 

        bool r = DeleteVolumeMountPoint(mountPoint);

        if (!r)

            throw new Win32Exception(Marshal.GetLastWin32Error());

    }

 

    private static void Main(string[] args)

    {

        Console.WriteLine("Managed Unix-style mount program");

 

        if (args.Length != 2) {

            Usage();

            return;

        }

 

        bool unmount = false;

        String volumeName = null;

        String mountPoint = args[1];

       

        if (args[0].Equals("-u") || args[0].Equals("/u"))

            unmount = true;

        else {

            volumeName = args[0];

 

            if (volumeName[volumeName.Length - 1] != Path.DirectorySeparatorChar)

                volumeName += Path.DirectorySeparatorChar;

        }

 

        if (mountPoint[mountPoint.Length - 1] != Path.DirectorySeparatorChar)

            mountPoint += Path.DirectorySeparatorChar;

 

        if (unmount)

            Unmount(mountPoint);

        else

            Mount(volumeName, mountPoint);

    }

 

}