Mounting a "virtual drive"

  • Thread starter Thread starter Luc The Perverse
  • Start date Start date
L

Luc The Perverse

Hi - I have very little C# programming experience.

I am making a software product which calls for an interface almost identical
to Windows Explorer - and I wondered if mounting a "virtual drive" would be
feasable for me (someone not good at programming) to implement.
 
Luc said:
Hi - I have very little C# programming experience.

I am making a software product which calls for an interface almost
identical to Windows Explorer - and I wondered if mounting a "virtual
drive" would be feasable for me (someone not good at programming) to
implement.

Hi,

Unfortunately, this sort of thing is quite complex, and there are a couple
of approaches to what you're trying to do, or at least to get a similar
effect. A few that spring to mind are:

1. Creating a shell extension
There are quite a lot of examples around that show how to create a windows
shell extension. While this is not creating a virtual drive (i.e. you
wouldn't be able to access it from software as a drive letter) it will
create an entry in "My Computer", and subsequent applications that are
designed to work with the Windows shell will see this.

2. Create a device driver
Something you can definitely not achieve in C#, and a very complex feat to
implement using the Windows DDK.

3. Using the Windows API to create a drive that maps to a folder in local
storage somewhere. This does create a real drive letter.

The third option above is the simplest, and gets most of your functionality.
What it does is create a virtual drive and assigns it a real drive letter.
The contents of the drive will be the folder which you map it to. This
does of course mean you need a folder in local storage somewhere, which
your application will then fill with files/directories.

IMHO, the first one is your best bet if you want to get a "virtual drive"
effect in Windows explorer, and want programmatic control over the
contents, but bear in mind these two disadvantages:

1. It isn't a 'real' drive, and won't be assigned a drive letter.
2. It requires COM interop, and complicated programming with the Windows
shell

The third one is your best bet if you don't mind creating a directory
somewhere in your local storage then using your program to map a drive
letter to that directory, and then managing the contents dynamically, by
means of creating/modifying/deleting files.

To get you started, check out this example (for shell namespace extensions):
http://tinyurl.com/3o85o

Unfortunately, I don't own Windows myself and I can't remember the name of
the API that's used to map a drive letter to a path... perhaps a Google
will turn up something.
 
Luc said:
Hi - I have very little C# programming experience.

I am making a software product which calls for an interface almost
identical to Windows Explorer - and I wondered if mounting a "virtual
drive" would be feasable for me (someone not good at programming) to
implement.

Hi,

After a quick Google, the API I was talking about is DefineDosDevice. :-)
 
Tom Spink said:
Hi,

After a quick Google, the API I was talking about is DefineDosDevice. :-)

Hmm - I will check it out. Thank you for both your responses sir.
 
This class might help. The main method you're after is the
MapFolderToDrive() method, which you pass a drive letter and a folder name
to. It creates a logical drive.

You'll also want to have a look at the DriveInfo class, which will let you
get a list of used drive letters.

If you want the complete solution, I've uploaded it to:
http://www.sabarnett.co.uk/downloads/MapFolder.zip

This is a simple app that allows you to create drive mappings interactively.
It can also be run at startup to re-map drives on reboot. It's probably not
the highest quality as it was one of my first apps, but it illustrates the
methods needed to map a drive.

HTH
Steve



using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Lucidus.MapFolder
{
class VolumeFunctions
{
[DllImport("kernel32.dll")]
internal static extern bool DefineDosDevice(uint dwFlags, string
lpDeviceName,
string lpTargetPath);

[DllImport("Kernel32.dll")]
internal static extern uint QueryDosDevice(string lpDeviceName,
StringBuilder lpTargetPath, uint ucchMax);

internal const uint DDD_RAW_TARGET_PATH = 0x00000001;
internal const uint DDD_REMOVE_DEFINITION = 0x00000002;
internal const uint DDD_EXACT_MATCH_ON_REMOVE = 0x00000004;
internal const uint DDD_NO_BROADCAST_SYSTEM = 0x00000008;

const string MAPPED_FOLDER_INDICATOR = @"\??\";

// ----------------------------------------------------------------------------------------
// Class Name: VolumeFunctions
// Procedure Name: MapFolderToDrive
// Purpose: Map the folder to a drive letter
// Parameters:
// - driveLetter (string) : Drive letter in the format "C:"
without a back slash
// - folderName (string) : Folder to map without a back slash
// ----------------------------------------------------------------------------------------
internal static string MapFolderToDrive(string driveLetter, string
folderName)
{
// Is this drive already mapped? If so, we don't remap it!
StringBuilder volumeMap = new StringBuilder(1024);
QueryDosDevice(driveLetter, volumeMap, (uint)1024);
if (volumeMap.ToString().StartsWith(MAPPED_FOLDER_INDICATOR) ==
true)
return "Volume is already mapped - map not changed";

// Map the folder to the drive
DefineDosDevice(0, driveLetter, folderName);

// Display a status message to the user.
string statusMessage = new
Win32Exception(Marshal.GetLastWin32Error()).ToString();
return statusMessage.Substring(statusMessage.IndexOf(":") + 1);
}

// ----------------------------------------------------------------------------------------
// Class Name: VolumeFunctions
// Procedure Name: UnmapFolderFromDrive
// Purpose: Unmap a drive letter. We always unmp the drive,
without checking the
// folder name.
// Parameters:
// - driveLetter (string) : Drive letter to be released, the the
format "C:"
// - folderName (string) : Folder name that the drive is mapped
to.
// ----------------------------------------------------------------------------------------
internal static string UnmapFolderFromDrive(string driveLetter,
string folderName)
{
DefineDosDevice(DDD_REMOVE_DEFINITION, driveLetter, folderName);

// Display the status of the "last" unmap we run.
string statusMessage = new
Win32Exception(Marshal.GetLastWin32Error()).ToString();
return statusMessage.Substring(statusMessage.IndexOf(":") + 1);
}

// ----------------------------------------------------------------------------------------
// Class Name: VolumeFunctions
// Procedure Name: DriveIsMappedTo
// Purpose: Returns the folder that a drive is mapped to. If not
mapped, we return a blank.
// Parameters:
// - driveLetter (string) : Drive letter in the format "C:"
// ----------------------------------------------------------------------------------------
internal static string DriveIsMappedTo(string driveLetter)
{
StringBuilder volumeMap = new StringBuilder(512);
string mappedVolumeName = "";

// If it's not a mapped drive, just remove it from the list
uint mapped = QueryDosDevice(driveLetter, volumeMap, (uint)512);
if (mapped != 0)
if (volumeMap.ToString().StartsWith(MAPPED_FOLDER_INDICATOR)
== true)
{
// It's a mapped drive, so return the mapped folder name
mappedVolumeName = volumeMap.ToString().Substring(4);
}

return mappedVolumeName;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top