2 jixiangsichuan jixiangsichuan 于 2014.04.18 14:05 提问

C#操作Hyper-V类库,设置虚拟机硬盘路径,以及重装系统等操作

如题,在MSDN里面找到的安装虚拟硬盘路径, 但是Utility和ReturnCode这两个类没有引用,也不知道从何处来的。请教大神如何解答
using System;
using System.Management;

namespace HyperVSamples
{
class MountVHD
{
static void Mount(string path)
{
ManagementScope scope = new ManagementScope(@"root\virtualization", null);
ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");

        ManagementBaseObject inParams = imageService.GetMethodParameters("Mount");
        inParams["Path"] = path;
        ManagementBaseObject outParams = imageService.InvokeMethod("Mount", inParams, null);
        if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
        {
            if (Utility.JobCompleted(outParams, scope))
            {
                Console.WriteLine("{0} was mounted successfully.", inParams["Path"]);
            }
            else
            {
                Console.WriteLine("Unable to mount {0}", inParams["Path"]);
            }
        }
        inParams.Dispose();
        outParams.Dispose();
        imageService.Dispose();
    }

    static void Main(string[] args)
    {
        if (args != null && args.Length != 1)
        {
            Console.WriteLine("Usage: MountVHD vhdPath");
            return;
        }
        Mount(args[0]);
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片