【问题标题】:DiscUtils create an NTFS vhdDiscUtils 创建 NTFS vhd
【发布时间】:2019-02-19 08:46:48
【问题描述】:

我正在尝试在 .NET Core 中使用 NTFS 创建虚拟硬盘。

我发现 DiscUtils NuGet 包及其GitHub page 上的示例代码可以很好地创建具有 FAT 格式的 VHD。

long diskSize = 30 * 1024 * 1024; //30MB
using (Stream vhdStream = File.Create(@"C:\TEMP\mydisk.vhd"))
{
    Disk disk = Disk.InitializeDynamic(vhdStream, diskSize);
    BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsFat);
    using (FatFileSystem fs = FatFileSystem.FormatPartition(disk, 0, null))
    {
        fs.CreateDirectory(@"TestDir\CHILD");
        // do other things with the file system...
    }
}

但对于我的用例,我需要大于 2 GB 的文件。因为无论如何我们都在使用 Windows,所以 NTFS 没问题。所以我尝试了这段代码

long diskSize = 300 * 1024 * 1024; //300 MB
var vhdPath = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), "vhd"));

using (Stream vhdStream = File.Create(vhdPath))
{
    var disk = DiscUtils.Vhd.Disk.InitializeFixed(vhdStream, Ownership.None, diskSize);
    BiosPartitionTable.Initialize(disk, WellKnownPartitionType.WindowsNtfs);
    using (var ntfs = NtfsFileSystem.Format(vhdStream, "Virtual NTFS drive", Geometry.FromCapacity(diskSize), 0, diskSize / Sizes.Sector))
    {
        ntfs.CreateDirectory(@"TestDir\CHILD");

        // do other things with the file system...
    }
}

此代码创建了一个 300 MB VHD,我可以使用 7zip 打开它,但它包含一个 ~300 MB *.mbr 文件。如果我尝试打开它,它会在临时文件夹中打开一个新的 7zip 窗口。 如果我对该 vhd 进行双时钟,则会收到 Windows 错误 “驱动器映像未初始化,包含无法识别的分区或包含未分配给驱动器号的卷。 使用驱动器管理管理单元确保驱动器、分区和卷处于可用状态。”(免费翻译自德语)

之后我无法再访问该文件,因为某些 Windows 进程仍在忙。

我在这里误解了什么?

还有其他方法可以使用 C# 和 .NET Core 创建/安装 VHD?

【问题讨论】:

标签: c# .net-core ntfs vhd


【解决方案1】:

我从 EricZimmerman 那里得到了关于 GitHub page 项目的答案:

var diskSize = 2000 * 1024 * 1024;
var asVhd = false;

using (var fs = new FileStream(_vhfxFileName, FileMode.OpenOrCreate))
{
    VirtualDisk destDisk = Disk.InitializeDynamic(fs, Ownership.None, diskSize);

    if (asVhd)
    {
        destDisk = DiscUtils.Vhd.Disk.InitializeDynamic(fs, Ownership.None, diskSize);
    }

    BiosPartitionTable.Initialize(destDisk, WellKnownPartitionType.WindowsNtfs);
    var volMgr = new VolumeManager(destDisk);

    var label = $"ZZZZZZ ({dateStamp})";

    using (var destNtfs = NtfsFileSystem.Format(volMgr.GetLogicalVolumes()[0], label, new NtfsFormatOptions()))
    {
        destNtfs.NtfsOptions.ShortNameCreation = ShortFileNameOption.Disabled;

        var destDirectory = Path.GetDirectoryName(@"Some file");
        destNtfs.CreateDirectory(destDirectory, nfo);

        using (Stream source = new FileStream(@"Some file2", FileMode.Open, FileAccess.Read))
        {
            var destFileName = @"foo";

            using (Stream dest = destNtfs.OpenFile(destFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                source.CopyTo(dest);
                dest.Flush();
            }
        }
        // do more stuff here
    }

    // commit everything to the stream before closing
    fs.Flush();
}

为我工作!享受:)

【讨论】:

    猜你喜欢
    • 2013-07-18
    • 2017-11-06
    • 2017-02-08
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2017-01-28
    相关资源
    最近更新 更多