【发布时间】: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?
【问题讨论】:
-
也许你会从 GitHub 页面上的作者那里得到更快的回复
-
我现在做到了。 github.com/DiscUtils/DiscUtils/issues/137我希望这是适合它的地方..