我正在编写一个程序,它需要解析 Windows 命令“diskpart”的输出结果,特别是在用 VB.NET 解析磁盘和卷信息(使用“list disk”和“list volume”)时
我设法使用 diskpart 将结果输出到文本文件中,方法是使用 dos 命令 diskpart/s myscript.scp > result.txt
现在在 VB.NET 中,我需要解析“result.txt”以将“卷号”、“驱动器号”、“驱动器标签”、“类型”、“大小”和“大小单位”检索到数组中以供进一步使用加工。
检索所有这些数据的最佳方法是什么?我尝试在 MSDN 上阅读有关 .NET 正则表达式 (Regex) 的内容,这让我感到困惑并且不确定在我的案例中应该使用什么模式。
希望有人能帮助我,欢迎任何替代的解析方法。
*2013 年 6 月 26 日更新 - 由于某些技术原因,我需要坚持使用“diskpart”并依赖它的输出结果进行解析。
输入示例(result.txt)
Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: PC1
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 1863 GB 1024 KB
Disk 1 No Media 0 B 0 B
Disk 2 Online 7424 MB 0 B
Volume ### Ltr Label Fs Type Size Status Info
---------- --- ----------- ----- ---------- ------- --------- --------
Volume 0 E DVD-ROM 0 B No Media
Volume 1 C SYSTEM NTFS Partition 100 GB Healthy System
Volume 2 D TEMP NTFS Partition 1606 GB Healthy Pagefile
Volume 3 G Removable 0 B No Media
Volume 4 F GSFKEY NTFS Removable 7423 MB Healthy
输出
disk(0)
.disknum=0
.size=1863
.size_unit="GB
disk(0)
.disknum=1
.size=0
.size_unit="B"
..and so on
vol(0)
.volnum=0
.letter="E"
.label=""
.type="DVD-ROM"
.size=0
.size_unit="B"
vol(1)
.volnum=1
.letter="C"
.label="SYSTEM"
.type="Partition"
.size=100
.size_unit="GB"
..and so on
最佳答案
你能从 System.IO.DriveInfo 得到这个信息吗?
public DriveInfo(string driveName);
public long AvailableFreeSpace
public string DriveFormat
public DriveType DriveType
public DirectoryInfo RootDirectory
public long TotalFreeSpace
public long TotalSize
public string VolumeLabel
public static DriveInfo[] GetDrives();
关于.net - 使用 .NET 解析 Diskpart 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314857/