VB.NET Process.Start() 立即停止
我在使用以下代码时遇到了一个奇怪的问题,我正在 VB.NET 中编写一个游戏启动器:
Dim gameProcess As Process = New Process()
gameProcess.StartInfo.UseShellExecute = False
gameProcess.StartInfo.FileName = TextBox2.Text
gameProcess.Start()
Debug.Print("Game started")
gameProcess.WaitForExit()
Debug.Print("Game stopped")
此代码对于大多数程序都可以正常工作,但对于其中一些程序(例如帝国时代),我得到以下结果:
游戏开始
游戏停止
我在任务管理器中看到游戏运行了一瞬间,但它立即关闭! 有人知道为什么吗?当我从 Windows 运行游戏时,它运行良好。
我也试过用Shell,同样的问题。
我尝试过使用 cmd.exe 和 /C 参数,同样的问题(请注意,当我在 Windows 运行对话框中键入 cmd.exe /C path_to_game_exe 时,游戏也会正常启动),只有当我从VB.NET 应用程序出现问题。
我的最后一个想法是编写一个临时批处理文件并启动该文件,但这似乎是一个丑陋的解决方案。
I'm having a strange problem with the following code, I'm writing a game launcher in VB.NET:
Dim gameProcess As Process = New Process()
gameProcess.StartInfo.UseShellExecute = False
gameProcess.StartInfo.FileName = TextBox2.Text
gameProcess.Start()
Debug.Print("Game started")
gameProcess.WaitForExit()
Debug.Print("Game stopped")
This code is working fine with most programs, but with some of them (Age of Empires e.g.), I get the following:
Game started
Game stopped
I see the game running for a split second in the task manager, but it immediately closes! Does anyone know why? When I run the game from Windows, it works fine.
I've also tried with Shell, same problem.
And I've tried with cmd.exe and the /C argument, same problem (note that when I type cmd.exe /C path_to_game_exe in the Windows Run Dialog, the game also starts fine), it's only when I launch it from the VB.NET app that it gives problems.
My last idea was to write a temporary batch file and starting that one, but that seems like an ugly solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试的另一个选项:在 startinfo.txt 文件中设置工作目录属性。 您可以通过查看开始菜单中游戏快捷方式的属性来找到此信息。
http://msdn.microsoft.com/en-我们/library/system.diagnostics.processstartinfo.workingdirectory.aspx
Another option you can try: set the working directory property in the startinfo. You can find this info by looking at the properties of the games' shortcut in your startmenu.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx
许多游戏使用启动器应用程序,在单独的进程中启动游戏,然后关闭。
A lot of games use a launcher app that starts the game in separate process and then closes.
它可能是复制保护检测系统上的调试器并退出进程。
尝试在发布模式下编译游戏并在 Visual Studio 未运行时运行应用程序。
It could be the copy protection that detects the debugger on your system and exits the process.
Try compiling the game in Release mode and run your application when Visual Studio is not running.