Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating virtual hard Drive

Tags:

how can I create Virtual Hard Drive (like Z:) that store it's files on physical hard drive (Like C:\Files).

like image 344
Ata Avatar asked Sep 20 '10 17:09

Ata


People also ask

What does Create a virtual hard disk mean?

A virtual hard disk (VHD) is a disk image file format for storing the entire contents of a computer's hard drive. The disk image, sometimes called a virtual machine (VM), replicates an existing hard drive, including all data and structural elements.

How do I Create a virtual drive in Windows 10?

Step 1: Right-click on the Windows icon on the desktop and then click Disk Management in the prompted menu. Step 2: Click on the Action tab in the Disk Management window and click Create VHD. Step 3: In the pop-up window, configure settings for the new virtual drive Windows 10. Click Browse.

Does Windows 10 have a virtual drive?

A virtual drive on Windows 10 can help you with disk management and has several benefits. Read how to create a virtual hard disk on Windows. In Windows 10, a virtual hard disk (VHDX or VHD) works in the same way as a physical hard drive but the hard drive is contained within the virtual system.


2 Answers

Here is C# code to do this directly:

using System; using System.Text; using System.ComponentModel; using System.Runtime.InteropServices;  static class Subst {     public static void MapDrive(char letter, string path) {         if (!DefineDosDevice(0, devName(letter), path))             throw new Win32Exception();     }     public static void UnmapDrive(char letter) {         if (!DefineDosDevice(2, devName(letter), null))             throw new Win32Exception();     }     public static string GetDriveMapping(char letter) {         var sb = new StringBuilder(259);         if (QueryDosDevice(devName(letter), sb, sb.Capacity) == 0) {             // Return empty string if the drive is not mapped             int err = Marshal.GetLastWin32Error();             if (err == 2) return "";             throw new Win32Exception();         }         return sb.ToString().Substring(4);     }       private static string devName(char letter) {         return new string(char.ToUpper(letter), 1) + ":";     }     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]     private static extern bool DefineDosDevice(int flags, string devname, string path);     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]     private static extern int QueryDosDevice(string devname, StringBuilder buffer, int bufSize); } 

Sample usage:

        Subst.MapDrive('z', @"c:\temp");         Console.WriteLine(Subst.GetDriveMapping('z'));         Subst.UnmapDrive('z'); 
like image 182
Hans Passant Avatar answered Sep 27 '22 22:09

Hans Passant


You can use subst command. Use System.Diagnostic.Process to run the subst.exe with desired parameters.

Here is the command syntax:

Syntax

Associates a path with a drive letter.

SUBST [drive1: [drive2:]path]  SUBST drive1: /D 

drive1: Specifies a virtual drive to which you want to assign a path.

[drive2:]path Specifies a physical drive and path you want to assign to a virtual drive.

/D Deletes a substituted (virtual) drive.

Type SUBST with no parameters to display a list of current virtual drives.list of current virtual drives.

like image 29
Andrea Parodi Avatar answered Sep 27 '22 23:09

Andrea Parodi