how can I create Virtual Hard Drive (like Z:) that store it's files on physical hard drive (Like C:\Files).
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.
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.
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.
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With