2011-04-23 50 views
1

我相信我一直在采取正确的方法到目前为止,但我希望有一个按钮在我的电脑上启动视频游戏。C#Windows应用程序 - 链接一个按钮以启动游戏

到目前为止,我有一个按钮链接到一个过程:

void button2_Click(object sender, EventArgs e) 
{ 
    process1.Start(); 
} 

this.process1.EnableRaisingEvents = true; 
this.process1.StartInfo.Domain = ""; 
this.process1.StartInfo.FileName = "MBO\\marbleblast.exe"; 
this.process1.StartInfo.LoadUserProfile = false; 
this.process1.StartInfo.Password = null; 
this.process1.StartInfo.StandardErrorEncoding = null; 
this.process1.StartInfo.StandardOutputEncoding = null; 
this.process1.StartInfo.UserName = ""; 
this.process1.SynchronizingObject = this; 
this.process1.Exited += new System.EventHandler(this.Process1Exited); 

所以,在这里,我曾经放置EXE(在一个我编码),将推出子文件夹MBO下的“marbleblast.exe”相对于它的位置。

它似乎在工作,并试图启动游戏,但是,它说它无法加载那里的文件。我在没有我的发射器的情况下测试了游戏,并且它工作正常。我相信它试图运行EXE,但不让它使用其文件夹内的其他文件。

如果需要,我会给出更多细节。

如何让游戏正常运行?

回答

1

this.process1.StartInfo.WorkingDirectory = “MBO \”;

在游戏中存在sl programming的编程,它依赖于Environment.CurrentDirectory被设置正确。默认情况下,它与EXE所在的目录相同。 upvoted的答案虽然重复了错误。为了使这个声明确实解决了这个问题,你现在依靠你的 CurrentDirectory被设置正确。如果它没有设置在你认为的地方,那么它仍然无法工作。

程序当前目录的问题是它可以被你不控制的软件改变。经典示例是OpenFileDialog,其RestoreDirectory属性设置为默认值false。等等。

总是防守地编程并传递文件和目录的完整路径名。像c:\ mumble \ foo.ext一样。为了实现这一目标,请从Assembly.GetEntryAssembly()。位置开始,这是EXE的路径。然后使用System.IO.Path类从中生成路径名称。正确的始终工作代码是:

using System.IO; 
using System.Reflection; 
... 
     string myDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 
     string gameDir = Path.Combine(myDir, "MBO"); 
     string gameExe = Path.Combine(gameDir, "marbleblast.exe"); 
     process1.StartInfo.FileName = gameExe; 
     process1.StartInfo.WorkingDirectory = gameDir; 
     process1.SynchronizingObject = this; 
     process1.EnableRaisingEvents = true; 
     process1.Exited += new EventHandler(Process1Exited); 
+0

仍然没有工作,虽然我得到的基本概念。编辑:它调整了一些调整后!非常感谢! – 2011-04-23 16:10:50

2

尝试添加此类似,设置Working Directory

this.process1.StartInfo.WorkingDirectory= "MBO\\"; 

什么的。

+0

一旦我这样做,它给了我一个错误,结果似乎比以前更糟糕。 – 2011-04-23 15:49:04

1

我是来自MBForums的Dobrakmato。你简单的需要添加大理石爆炸的工作目录。

this.process1.EnableRaisingEvents = true; 
this.process1.StartInfo.Domain = ""; 
this.process1.StartInfo.FileName = "MBO\\marbleblast.exe"; 
this.process1.StartInfo.WorkingDirectory = "pathto marbleblast.exe directory"; 
this.process1.StartInfo.LoadUserProfile = false; 
this.process1.StartInfo.Password = null; 
this.process1.StartInfo.StandardErrorEncoding = null; 
this.process1.StartInfo.StandardOutputEncoding = null; 
this.process1.StartInfo.UserName = ""; 
this.process1.SynchronizingObject = this; 
this.process1.Exited += new System.EventHandler(this.Process1Exited); 
+0

嘿Dobrakmato,不知道你在这里!我遇到了一些问题。你的解决方案和上面的解决方案都不会产生结果。 – 2011-04-23 15:54:52

+0

我在开发MBMC时发现了这个解决方案。 WorkingDir也要求Torque Constructor和Gimp。 – 2011-04-23 16:00:04