Virtual Drive

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi ,
Can any one tel me how to create a virtual drive in C#( similar to
Gmail Virtual drive).Also tell me what interfaces could be used to create the
same.

Thanks in Advance
Kavitha
 
Don't know anything about GMail or it's virtual drive facilities, so this
may not help in the slightest. I have a class that I use on the local PC to
map a folder to a drive letter, so I can, for example, map
d:\dev\source\MSVC\Tree to a virtual "j:" drive. Is that what's wanted?

I've reproduced the code below, just in case it has some relevance...

Steve

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

namespace 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