启动虚拟机“Windows XP模式”;通过c#程序Windows 7

本文关键字:Windows 通过 程序 模式 虚拟机 XP 启动 | 更新日期: 2023-09-27 18:03:02

好的,所以我在通过c#程序启动Windows XP模式VM时遇到了问题。我使用的命令是"vmwindow -file "absolute path to vmcx file",但问题是该命令不能与我的程序启动的cmd提示符一起工作。所以,这很奇怪。我可以去我的计算机上的命令提示符,在我的计算机上运行这个命令,它可以工作,但是如果我在我的c#程序上有相同的命令,弹出的命令提示符告诉我"vmwindow"不是一个可识别的命令。我甚至查看了每个命令提示符的路径,它们是不同的,但它们仍然都包含"C:'Windows'system32'",这是vmwindow.exe存在的地方。所以,我在我的程序填充的命令提示符窗口上导航,文件"vmwindow.exe"不在那里,但是如果我从计算机打开命令提示符窗口并导航到该文件夹,它就存在于那里。我想不出其他任何东西,因为我已经确保它们都在管理员模式下运行,而且我还尝试启动一个包含该命令的bat文件,而不是直接运行该命令。希望有人知道这事。下面是我使用的代码:

private void button1_Click(object sender, EventArgs e)
    { 
        Process process = new System.Diagnostics.Process();
        ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        startInfo.FileName = "cmd.exe";
        startInfo.WorkingDirectory = @"<my path>";
        startInfo.Arguments = "/k vmwindow.exe -file '"<path to vcmx file>''Windows XP Mode.vmcx'"";
        process.StartInfo = startInfo;
        process.Start();
    }

启动虚拟机“Windows XP模式”;通过c#程序Windows 7

你可以使用Powershell。它具有Hyper V控制的本地集成,并且很容易从c#

调用。

你可以在这里看到所有的hv -cmdlet

启动机器的一个简单命令是

Start-VM "Windows 8.1 Pro" -Computername HV-Host1
// etcetc
Stop-VM "Windows 8.1 Pro" -Save

在c#中应该是这样的

using (PowerShell PowerShellInstance = PowerShell.Create())
{
    PowerShellInstance.AddScript("Start-VM "Windows 8.1 Pro" -Computername HV-Host1");
}

可能是因为您编译程序时使用的位设置。("Platform target"answers"Prefer 32-bit"设置在项目的build选项卡下)。32位和64位进程在System32下看到不同的文件。见https://stackoverflow.com/a/950011