C#中WMI挂载ISO时如何获取分配的盘符?
How Do I Get the Assigned Drive Letter When Mounting an ISO with WMI in C#?
这是我用来挂载 ISO 的代码
// With help of WMICodeCreator
ManagementObject mo = new ManagementObject("root\Microsoft\Windows\Storage",
"MSFT_DiskImage.ImagePath='" + isoFile + "',StorageType=1",
null);
// Obtain in-parameters for the method
ManagementBaseObject inParams = mo.GetMethodParameters("Mount");
// Execute the method and obtain the return values.
ManagementBaseObject outParams = mo.InvokeMethod("Mount", inParams, null);
outParams 只是 returns 记录的布尔值 here。
如果我发出这个命令:
mo.Get();
string device = mo.GetPropertyValue("DevicePath");
...设备字符串显示\.\CDROM0
。即使我安装第二个 ISO,它也会显示此值。
我在 Jim Dale 创建的 C++ 项目中找到了解决方案 MountISO.wmi on GitHub
这是我最终的 C# class,用于在 Windows 8+ 中自动安装和卸载 iso 映像并检索 auto-mounted 驱动器号。它比典型的 PowerShell 方法更快、更可靠。
using System.Management;
using System.Threading;
namespace IsoTools
{
public static class IsoMounter
{
private const string WmiScope = @"root\Microsoft\Windows\Storage";
/// <summary>Mounts an ISO disc image file.</summary>
/// <param name="isoPath">
/// The full path of the ISO to be mounted.
/// </param>
/// <returns>
/// A System.Char representing a drive volume letter.
/// </returns>
public static char Mount(string isoPath)
{
string isoObjectPath = BuildIsoObjectPath(isoPath);
using (var isoObject =
new ManagementObject(WmiScope, isoObjectPath, null))
{
using (ManagementBaseObject inParams =
isoObject.GetMethodParameters("Mount"))
{
isoObject.InvokeMethod("Mount", inParams, null);
}
}
// The query used to retrieve the volume letter for an image.
string volumeQuery = "ASSOCIATORS OF {" + isoObjectPath + "}" +
"WHERE AssocClass = MSFT_DiskImageToVolume " +
"ResultClass = MSFT_Volume";
char mountLetter = '[=10=]';
using (var query =
new ManagementObjectSearcher(WmiScope, volumeQuery))
{
// Run query until drive is mounted
while (mountLetter < 65)
{
Thread.Sleep(50);
using (ManagementObjectCollection queryCollection =
query.Get())
{
foreach (ManagementBaseObject item in queryCollection)
{
mountLetter = item["DriveLetter"].ToString()[0];
}
}
}
}
return mountLetter;
}
/// <summary>Dismount an ISO disc image file.</summary>
/// <param name="isoPath">
/// The full path of the ISO to be mounted.
/// </param>
public static void Dismount(string isoPath)
{
using (var isoObject = new ManagementObject(
WmiScope,
BuildIsoObjectPath(isoPath),
null))
{
using (ManagementBaseObject inParams =
isoObject.GetMethodParameters("Dismount"))
{
isoObject.InvokeMethod("Dismount", inParams, null);
}
}
}
/// <summary>Creates the WMI pathstring for an ISO image.</summary>
/// <param name="isoPath">
/// The full path of the ISO to be mounted.
/// </param>
/// <returns>A System.String representing a WMI pathstring.</returns>
private static string BuildIsoObjectPath(string isoPath)
{
// Single quoted paths do not allow escaping of single quotes
// within the ImagePath. Use double quotes and escape backslashes.
return "MSFT_DiskImage.ImagePath=\"" +
isoPath.Replace("\", "\\") +
"\",StorageType=1";
}
}
}
这是我用来挂载 ISO 的代码
// With help of WMICodeCreator
ManagementObject mo = new ManagementObject("root\Microsoft\Windows\Storage",
"MSFT_DiskImage.ImagePath='" + isoFile + "',StorageType=1",
null);
// Obtain in-parameters for the method
ManagementBaseObject inParams = mo.GetMethodParameters("Mount");
// Execute the method and obtain the return values.
ManagementBaseObject outParams = mo.InvokeMethod("Mount", inParams, null);
outParams 只是 returns 记录的布尔值 here。
如果我发出这个命令:
mo.Get();
string device = mo.GetPropertyValue("DevicePath");
...设备字符串显示\.\CDROM0
。即使我安装第二个 ISO,它也会显示此值。
我在 Jim Dale 创建的 C++ 项目中找到了解决方案 MountISO.wmi on GitHub
这是我最终的 C# class,用于在 Windows 8+ 中自动安装和卸载 iso 映像并检索 auto-mounted 驱动器号。它比典型的 PowerShell 方法更快、更可靠。
using System.Management;
using System.Threading;
namespace IsoTools
{
public static class IsoMounter
{
private const string WmiScope = @"root\Microsoft\Windows\Storage";
/// <summary>Mounts an ISO disc image file.</summary>
/// <param name="isoPath">
/// The full path of the ISO to be mounted.
/// </param>
/// <returns>
/// A System.Char representing a drive volume letter.
/// </returns>
public static char Mount(string isoPath)
{
string isoObjectPath = BuildIsoObjectPath(isoPath);
using (var isoObject =
new ManagementObject(WmiScope, isoObjectPath, null))
{
using (ManagementBaseObject inParams =
isoObject.GetMethodParameters("Mount"))
{
isoObject.InvokeMethod("Mount", inParams, null);
}
}
// The query used to retrieve the volume letter for an image.
string volumeQuery = "ASSOCIATORS OF {" + isoObjectPath + "}" +
"WHERE AssocClass = MSFT_DiskImageToVolume " +
"ResultClass = MSFT_Volume";
char mountLetter = '[=10=]';
using (var query =
new ManagementObjectSearcher(WmiScope, volumeQuery))
{
// Run query until drive is mounted
while (mountLetter < 65)
{
Thread.Sleep(50);
using (ManagementObjectCollection queryCollection =
query.Get())
{
foreach (ManagementBaseObject item in queryCollection)
{
mountLetter = item["DriveLetter"].ToString()[0];
}
}
}
}
return mountLetter;
}
/// <summary>Dismount an ISO disc image file.</summary>
/// <param name="isoPath">
/// The full path of the ISO to be mounted.
/// </param>
public static void Dismount(string isoPath)
{
using (var isoObject = new ManagementObject(
WmiScope,
BuildIsoObjectPath(isoPath),
null))
{
using (ManagementBaseObject inParams =
isoObject.GetMethodParameters("Dismount"))
{
isoObject.InvokeMethod("Dismount", inParams, null);
}
}
}
/// <summary>Creates the WMI pathstring for an ISO image.</summary>
/// <param name="isoPath">
/// The full path of the ISO to be mounted.
/// </param>
/// <returns>A System.String representing a WMI pathstring.</returns>
private static string BuildIsoObjectPath(string isoPath)
{
// Single quoted paths do not allow escaping of single quotes
// within the ImagePath. Use double quotes and escape backslashes.
return "MSFT_DiskImage.ImagePath=\"" +
isoPath.Replace("\", "\\") +
"\",StorageType=1";
}
}
}