C#Windows应用程序-链接按钮开始游戏

C#Windows应用程序-链接按钮开始游戏,c#,executable,launcher,launching-application,C#,Executable,Launcher,Launching Application,到目前为止,我相信我已经采取了正确的方法,但我希望在我的电脑上有一个启动视频游戏的按钮 到目前为止,我有一个链接到流程的按钮: void button2_Click(object sender, EventArgs e) { process1.Start(); } 因此,无论我在哪里放置EXE(我正在编写的代码),它都会在子文件夹MBO下相对于其位置启动“marbleblast.EXE” 它似乎正在工作,并试图启动游戏,但是,它说它无法加载存在那里的文件。我在没有发射器的情况下测试了

到目前为止,我相信我已经采取了正确的方法,但我希望在我的电脑上有一个启动视频游戏的按钮

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

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

因此,无论我在哪里放置EXE(我正在编写的代码),它都会在子文件夹MBO下相对于其位置启动“marbleblast.EXE”

它似乎正在工作,并试图启动游戏,但是,它说它无法加载存在那里的文件。我在没有发射器的情况下测试了这个游戏,它成功了。我相信它正在尝试运行EXE,但不允许它使用其文件夹中的其他文件

如果需要,我会提供更多细节

如何使游戏正常运行?

尝试添加此选项

this.process1.StartInfo.WorkingDirectory= "MBO\\";
或类似于设置的内容。

尝试添加此项

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

或者类似的设置。

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

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

我是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);

我是MBF的多布拉克马托。您只需为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);
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);
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);

一旦我这样做了,它会给我一个错误,结果似乎比以前更糟。一旦我这样做了,它会给我一个错误,结果似乎比以前更糟。嘿,多布拉克马托,不知道你在这里!我对此有点问题。你的解决方案和你上面的解决方案都不能产生结果。我在开发MBMC时发现了这个解决方案。WorkingDir还需要扭矩构造器和Gimp。嘿,Dobrakmato,不知道你在这里!我对此有点问题。你的解决方案和你上面的解决方案都不能产生结果。我在开发MBMC时发现了这个解决方案。WorkingDir还需要Torque构造函数和Gimp。尽管我得到了基本概念,但仍然不起作用。编辑:经过一些调整后它工作了!非常感谢!虽然我得到了基本的概念,但仍然不起作用。编辑:经过一些调整后它工作了!非常感谢!