首页 > 代码库 > C#控制台打开VM虚拟机
C#控制台打开VM虚拟机
添加引用->VixCOM.dll (在vix文件夹下)
VixWrapper.cs
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;using VixCOM;namespace VMHelper{ class VixWrapper { VixCOM.IVixLib vixLib = null; ulong m_vixError; VixCOM.IHost m_hostHandle = null; VixCOM.IVM m_vmHandle = null; public ulong GetError() { return m_vixError; } public VixWrapper() { try { vixLib = new VixCOM.VixLibClass(); } catch (COMException comExc) { System.Diagnostics.Trace.WriteLine(comExc.Message + "\n"); throw; } } /// <summary> /// Creates a host handle /// </summary> /// <returns>true if succeeded, otherwise false</returns> public bool Connect(string hostName, string userName, string password) { int hostType = string.IsNullOrEmpty(hostName) ? VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION : VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_SERVER; int vixVersion = VixCOM.Constants.VIX_API_VERSION; vixVersion = -1; VixCOM.IJob jobHandle = vixLib.Connect(vixVersion, hostType, null, 0, userName, password, 0, null, null); int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object results = new object(); m_vixError = jobHandle.Wait(propertyIds, ref results); if (m_vixError == VixCOM.Constants.VIX_OK) { object[] objectArray = (object[])results; m_hostHandle = (VixCOM.IHost)objectArray[0]; return true; } return false; } /// <summary> /// Opens the virtual machine specified in vmxFilePath /// </summary> /// <param name=”vmxFilePath”>The virtual machine vmx file to open</param> /// <returns>true if succeeded, otherwise false</returns> public bool OpenVm(string vmxFilePath) { IJob jobHandle = m_hostHandle.OpenVM(vmxFilePath, null); int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object results = new object(); m_vixError = jobHandle.Wait(propertyIds, ref results); if (m_vixError == VixCOM.Constants.VIX_OK) { object[] objectArray = (object[])results; m_vmHandle = (VixCOM.IVM)objectArray[0]; return true; } return false; } /// <summary> /// Power on the virtual machine /// </summary> /// <returns>true if succeeded, otherwise false</returns> public bool PowerOn() { IJob jobHandle = m_vmHandle.PowerOn(VixCOM.Constants.VIX_VMPOWEROP_LAUNCH_GUI, null, null); m_vixError = jobHandle.WaitWithoutResults(); if (m_vixError == VixCOM.Constants.VIX_OK) { jobHandle = m_vmHandle.WaitForToolsInGuest(300, null); m_vixError = jobHandle.WaitWithoutResults(); } return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Starts a snapshot of a virtual machine /// </summary> /// <param name=”snapshot_name”>The name of the snapshot to start</param> /// <returns>true if succeeded, otherwise false</returns> public bool RevertToLastSnapshot() { ISnapshot snapshot = null; m_vixError = m_vmHandle.GetRootSnapshot(0, out snapshot); if (m_vixError == VixCOM.Constants.VIX_OK) { IJob jobHandle = m_vmHandle.RevertToSnapshot(snapshot, 0, null, null); m_vixError = jobHandle.WaitWithoutResults(); } return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Login to the virtual machine /// </summary> /// <returns>true if succeeded, otherwise false</returns> public bool LogIn(string username, string password) { IJob jobHandle = m_vmHandle.LoginInGuest(username, password, 0, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Creates the directory in the Virtual Machine /// </summary> /// <param name=”pathName”></param> /// <returns></returns> public bool CreateDirectoryInVm(string pathName) { IJob jobHandle = m_vmHandle.CreateDirectoryInGuest(pathName, null, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Copies a file from the host machine to the virtual machine /// </summary> /// <param name=”sourceFile”>The source file on the host machine</param>