C#控制台打开VM虚拟机

简介: 添加引用->VixCOM.dll (在vix文件夹下) VixWrapper.cs using System; using System.Collections.Generic; using System.

添加引用->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> /// <param name=”destinationFile”>The destination on the VM</param> /// <returns>true if succeeded, otherwise false</returns> public bool CopyFileToVm(string sourceFile, string destinationFile) { // // Copy files from host to guest // IJob jobHandle = m_vmHandle.CopyFileFromHostToGuest(sourceFile, destinationFile, 0, null, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Copies a file from the virtual machine to the host machine /// </summary> /// <param name=”sourceFile”>The source file on the virtual machine</param> /// <param name=”destinationFile”>The destination on the host machine</param> /// <returns>true if succeeded, otherwise false</returns> public bool CopyFileFromVm(string sourceFile, string destinationFile) { // // Copy files from host to guest // IJob jobHandle = m_vmHandle.CopyFileFromGuestToHost(sourceFile, destinationFile, 0, null, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Runs a program on the virtual machine /// </summary> /// <param name=”exePath”>The path of the program on the virtual machine</param> /// <param name=”parameters”>The parameters to pass to the executable</param> /// <param name=”resultCode”>The result code returned from the program that ran on the VM</param> /// <returns>true if succeeded, otherwise false</returns> public bool RunProgram(string exePath, string parameters, out int resultCode) { resultCode = -1; IJob jobHandle = m_vmHandle.RunProgramInGuest(exePath, parameters, VixCOM.Constants.VIX_RUNPROGRAM_ACTIVATE_WINDOW, null, null); // clientData int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_GUEST_PROGRAM_EXIT_CODE }; object results = new object(); m_vixError = jobHandle.Wait(propertyIds, ref results); if (m_vixError == VixCOM.Constants.VIX_OK) { object[] objectArray = (object[])results; resultCode = (int)objectArray[0]; return true; } return false; } /// <summary> /// Power off the virtual machine /// </summary> /// <returns>true if succeeded, otherwise false</returns> public bool PowerOff() { IJob jobHandle = m_vmHandle.PowerOff(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// Restart the virtual machine /// </summary> /// <returns>true if succeeded, otherwise false</returns> public bool Restart() { IJob jobHandle = m_vmHandle.Reset(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } } }

 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using VMHelper;


namespace VixWrapperTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //VixWrapper.VixWrapper vix = new VixWrapper.VixWrapper();  
                VixWrapper wrapper = new VixWrapper();
  
                wrapper.Connect(null, "Administrator", null);

                wrapper.OpenVm(@"E:\win xp\Windows XP Professional.vmx");//安装好的虚拟机.vmx的实际路径
               
                wrapper.PowerOn();

                wrapper.PowerOff();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

    }
}


本程序实现了通过VS控制台打开vm虚拟机的功能,有兴趣的可以去调用剩下的函数,做个时延函数,然后完成其他任务。

 

遗失的拂晓
目录
相关文章
|
4月前
|
开发框架 .NET C#
C# 一分钟浅谈:第一个 C# 控制台应用程序
【9月更文挑战第1天】C# 是一种现代化的、面向对象的编程语言,广泛应用于桌面应用、Web 应用和游戏开发等领域。本文详细介绍如何创建第一个 C# 控制台应用程序,包括使用 Visual Studio 和 .NET SDK 的步骤,并解析常见问题及其解决方法,如控制台窗口立即关闭、编译错误和运行时错误等。通过实践,你将掌握 C# 控制台应用的基础知识,为进一步学习打下坚实基础。
331 48
|
5月前
|
虚拟化
如何为VM虚拟机添加D盘
这篇文章介绍了如何在VMware虚拟机中将E盘修改为D盘,并提供了详细的操作步骤,包括在计算机管理中更改驱动器号和路径,以及初始化和格式化新磁盘的指导。
如何为VM虚拟机添加D盘
|
5月前
|
虚拟化
VM虚拟机只有一个C盘怎么添加硬盘新分区盘符
关于如何在VMware虚拟机中解决只有一个C盘的问题,介绍了增加硬盘和创建新分区的方法。
VM虚拟机只有一个C盘怎么添加硬盘新分区盘符
|
5月前
|
安全
如何修改Xshell中的字体颜色大小等样式 修改Vm虚拟机终端的字体颜色
这篇文章介绍了如何在Xshell中修改字体颜色、大小等样式,以及如何在虚拟机终端设置字体颜色,以实现统一和舒适的视觉效果。
如何修改Xshell中的字体颜色大小等样式 修改Vm虚拟机终端的字体颜色
|
5月前
|
安全 Windows
【Azure 环境】Azure 的PaaS服务如果涉及到安全漏洞问题后,我们如何确认所用服务的实例(VM:虚拟机)的操作系统已修复该补丁呢?
【Azure 环境】Azure 的PaaS服务如果涉及到安全漏洞问题后,我们如何确认所用服务的实例(VM:虚拟机)的操作系统已修复该补丁呢?
|
5月前
|
API 网络架构
【Azure Developer】如何通过Azure REST API 获取到虚拟机(VM)所使用的公共IP地址信息
【Azure Developer】如何通过Azure REST API 获取到虚拟机(VM)所使用的公共IP地址信息
|
5月前
|
存储 API 开发工具
【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法
【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法
|
5月前
|
Java 数据安全/隐私保护 Windows
【Azure Developer】使用Java代码启动Azure VM(虚拟机)
【Azure Developer】使用Java代码启动Azure VM(虚拟机)
|
8月前
|
Ubuntu Linux 虚拟化
VM虚拟机使用的实用技巧分享
VM虚拟机使用的实用技巧分享
56 1
|
7月前
|
IDE Java Shell
CloudStack中控制台虚拟机调试
CloudStack中控制台虚拟机调试