创建虚拟磁盘挂载点

发布于 2024-11-29 16:45:49 字数 1975 浏览 1 评论 0原文

我已使用 ImDisk 库和 .NET 包装器在我的 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

更新

FormatDrive 函数:

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;
}

I have used the ImDisk library with the .NET wrapper to create a Virtual Disk in my C# application. However, after I create the device, I apparently need to create a Mount Point as well for the device to actually show as a Drive Letter. I don't completely understand what is supposed to be supplied for it to create a Mount Point, but I believe this pertains more to Virtual devices than the library.

My Function:

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 Definition:

        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

UPDATE

FormatDrive Function:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2024-12-06 16:45:49

事实证明,在 CreateDevice() 中传递的参数存在一些问题,这使得它不会生成错误,但无法完全完成设置过程。

感谢您的帮助!

Turns out their were some issues with the parameters being passed in CreateDevice(), which was allowing it to not generate errors but not fully completing the setup process.

Thanks for the help!

爱的十字路口 2024-12-06 16:45:49

您必须在driveLetter参数末尾添加“:”

you must add ":" at the end of driveLetter parameter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文