我使用了带有 .NET 包装器的 ImDisk 库,在我的 C# 应用程序中创建了一个虚拟磁盘。但是,在创建设备之后,我显然还需要创建一个挂载点,以便设备实际显示为驱动器号。我不完全理解应该为它提供什么来创建挂载点,但我相信这更多地与虚拟设备有关,而不是库。
我的功能:
public bool CreateRAMDisk()
{
// Create Empty RAM Disk
char driveLetter = ImDiskAPI.FindFreeDriveLetter();
ImDiskAPI.CreateDevice(52428800, 0, 0, 0, 0, ImDiskFlags.DeviceTypeHD | ImDiskFlags.TypeVM, null, false, driveLetter.ToString(), ref deviceID, IntPtr.Zero);
string mountPoint = driveLetter + @":\Device\ImDisk0";
ImDiskAPI.CreateMountPoint(mountPoint, deviceID);
// Format the Drive for NTFS
if (FormatDrive(driveLetter.ToString(), "NTFS", true, 4096, "", false))
{
CreateMountPoint 定义:
public static void CreateMountPoint(string Directory, uint DeviceNumber);
//
// Summary:
// Creates a mount point for an ImDisk virtual disk on an empty subdirectory
// on an NTFS volume.
//
// Parameters:
// Directory:
// Path to an empty subdirectory on an NTFS volume
//
// DeviceNumber:
// Device number of an existing ImDisk virtual disk
更新
格式化驱动功能:
public static bool FormatDrive(string driveLetter, string fileSystem, bool quickFormat, int clusterSize, string label, bool enableCompression)
{
driveLetter = driveLetter + ":";
if (driveLetter.Length != 2 || driveLetter[1] != ':'|| !char.IsLetter(driveLetter[0]))
{
return false;
}
//query and format given drive
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'");
foreach (ManagementObject vi in searcher.Get())
{
vi.InvokeMethod( "Format", new object[] {fileSystem, quickFormat, clusterSize, label, enableCompression} );
}
return true;
}