简体   繁体   English

通过C#程序Windows 7以编程方式启动vm“ Windows XP Mode”

[英]Starting a vm “Windows XP Mode” programmatically through a C# program Windows 7

Ok, so I'm having an issue starting the the Windows XP mode VM through ac# program. 好的,所以在通过ac#程序启动Windows XP模式VM时遇到问题。 The command I'm using is vmwindow -file "absolute path to vmcx file " , but the problem is that the command does not work with the cmd prompt that my program kicks off. 我正在使用的命令是vmwindow -file "absolute path to vmcx file ”,但是问题是该命令不适用于我的程序启动的cmd提示符。 So, it's very weird. 所以,这很奇怪。 I can go to command prompt on my computer and run this command on my computer and it works, but if i have the same command on my c# program, the command prompt that pops up tells me the "vmwindow" is not a recognized command. 我可以在计算机上转到命令提示符,然后在计算机上运行此命令,它可以工作,但是如果我在c#程序中具有相同的命令,则弹出的命令提示符会告诉我“ vmwindow”不是可识别的命令。 I even looked at the paths of each of the command prompts and they're different, but they still both contain "C:\\Windows\\system32\\" which is where vmwindow.exe exists. 我什至查看了每个命令提示符的路径,它们的路径不同,但是它们仍然都包含“ C:\\ Windows \\ system32 \\”,这是vmwindow.exe所在的位置。 So, I navigate on the command prompt window that my program populated and the file "vmwindow.exe" is not there, but if I open a command prompt window from my computer and navigate to that folder, it exists there. 因此,我在程序所填充的命令提示符窗口中导航,并且文件“ vmwindow.exe”不存在,但是如果我从计算机中打开命令提示符窗口并导航至该文件夹,则该文件夹存在。 I can't think of anything else as I already made sure they're both running in administrator mode, and also i tried starting a bat file which contained that command instead of running the command directly. 我什么都没有想到,因为我已经确保它们都在管理员模式下运行,并且我尝试启动包含该命令的bat文件,而不是直接运行该命令。 Hope anyone knows anything about this. 希望有人对此有所了解。 Here is the code I'm using: 这是我正在使用的代码:

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();
    }

What you can do is using Powershell. 您可以做的是使用Powershell。 It has a native integration for Hyper V control and is easy to call from c# 它具有用于Hyper V控制的本机集成,可以从c#轻松调用

You can see all HV-cmdlets here 您可以在此处查看所有HV-cmdlet

a simple command to start your machine would be 一个简单的命令来启动您的机器是

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

So this should be something like this in C# 所以在C#中应该是这样的

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

Probably it's because of the bitness setting you compile your program with. 可能是因为编译程序时使用了位设置。 ("Platform target" and "Prefer 32-bit" settings under the build tab of the project). (项目的“构建”选项卡下的“平台目标”和“首选32位”设置)。 32 and 64 bit processes see different files under System32. 32位和64位进程在System32下可以看到不同的文件。 See https://stackoverflow.com/a/950011 参见https://stackoverflow.com/a/950011

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM