1

我相信到目前为止我一直在采取正确的方法,但我想在我的电脑上有个按钮来启动视频游戏。

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

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,但不让它使用其文件夹中的其他文件。

如果需要,我会提供更多详细信息。

怎样才能让游戏正常运行?

4

4 回答 4

2

尝试添加这个

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

或类似设置工作目录的东西。

于 2011-04-23T15:14:43.597 回答
1

将 的WorkingDirectory属性设置ProcessInfo为正确的目录。

于 2011-04-23T15:16:02.600 回答
1

我是来自 MBForums 的 Dobrakmato。您只需为 Marble Blast 添加工作目录。

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);
于 2011-04-23T15:18:12.290 回答
1

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

游戏中的编程很草率,它依赖于正确设置 Environment.CurrentDirectory。默认情况下,它与 EXE 所在的目录相同。赞成的答案虽然重复了错误。要使该声明真正解决问题,您现在需要正确设置 CurrentDirectory。如果它没有设置在您认为的位置,那么它仍然无法工作。

程序当前目录的问题在于它可以被您无法控制的软件更改。经典示例是 OpenFileDialog,其中 RestoreDirectory 属性设置为默认值 false。等等。

始终进行防御性编程并传递文件和目录的完整路径名。像 c:\mumble\foo.ext。为此,请从 Assembly.GetEntryAssembly().Location 开始,这是您的 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);
于 2011-04-23T16:04:58.790 回答