简体   繁体   English

Process.Start问题启动游戏

[英]Process.Start issue Launching my game

Currently I'm trying to program a launcher for my game. 目前,我正在尝试为我的游戏编写启动器。 My issue is, if you launch my game this way: 我的问题是,如果您以这种方式启动游戏:

System.Diagnostics.Process.Start(@"F:\Freak Mind Games\Projects 2013\JustShoot\game.bat");

I get an unhandled exception or could not find error. 我收到未处理的异常或找不到错误。

But if I do it this way: 但是,如果我这样做:

System.Diagnostics.Process.Start(@"game.bat");

It works. 有用。 Where is the issue ? 问题出在哪里?

I assume that the batch file resides in the same directory as your program launcher. 我假设该批处理文件与程序启动器位于同一目录中。 Determine its location automatically: 自动确定其位置:

string executableDir = Path.GetDirectoryName(Application.ExecutablePath);
Process.Start(Path.Combine(executableDir, "game.bat"));

Where 哪里

  • Application.ExecutablePath is the path of your executable, ie F:\\Freak Mind Games\\Projects 2013\\JustShoot\\justshoot.exe . Application.ExecutablePath是可执行文件的路径,即F:\\Freak Mind Games\\Projects 2013\\JustShoot\\justshoot.exe

  • Path.GetDirectoryName(...) gets the directory part of it, ie F:\\Freak Mind Games\\Projects 2013\\JustShoot . Path.GetDirectoryName(...)获取目录的一部分,即F:\\Freak Mind Games\\Projects 2013\\JustShoot

  • Path.Combine(executableDir, "game.bat") combines the directory with game.bat , ie F:\\Freak Mind Games\\Projects 2013\\JustShoot\\game.bat Path.Combine(executableDir, "game.bat")将目录与game.bat ,即F:\\Freak Mind Games\\Projects 2013\\JustShoot\\game.bat


Keep also in mind that when started from Visual Studio the executable path is "...\\bin\\Debug" or "...\\bin\\Release" . 还请记住,从Visual Studio启动时,可执行路径为"...\\bin\\Debug""...\\bin\\Release" Therefore you might want to remove these parts from the path if your batch file resides in the project directory. 因此,如果批处理文件位于项目目录中,则可能需要从路径中删除这些部分。

const string DebugDir = @"\bin\Debug";
const string ReleaseDir = @"\bin\Release";

string executableDir = Path.GetDirectoryName(Application.ExecutablePath);
if (executableDir.EndsWith(DebugDir)) {
    executableDir =
        executableDir.Substring(0, executableDir.Length - DebugDir.Length);
} else if (executableDir.EndsWith(ReleaseDir)) {
    executableDir =
        executableDir.Substring(0, executableDir.Length - ReleaseDir.Length);
}
Process.Start(Path.Combine(executableDir, "game.bat"));

UPDATE 更新

It is not a good idea the hard code the directories. 硬编码目录不是一个好主意。 Place the paths of the games to be launched in a text file in the same directory as your game launcher (eg "launch.txt"). 将要启动的游戏的路径放置在与游戏启动器相同的目录中的文本文件中(例如“ launch.txt”)。 Each line would contain a game that can be launched with the name of the game plus its path. 每行将包含一个可以使用游戏名称及其路径启动的游戏。 Like this: 像这样:

Freak Mind Games = F:\Freak Mind Games\Projects 2013\JustShoot\game.bat
Minecraft = C:\Programs\Minecraft\minecraft.exe

Define a directory as variable in your form: 在表单中将目录定义为变量:

private Dictionary<string,string> _games;

Now get a list of these games like this: 现在,获得这些游戏的列表,如下所示:

string executableDir = Path.GetDirectoryName(Application.ExecutablePath);
string launchFile = Path.Combine(executableDir, "launch.txt"));

string[] lines = File.ReadAllLines(launchFile);

// Fill the dictionary with the game name as key and the path as value.
_games = lines
    .Where(l => l.Contains("="))
    .Select(l => l.Split('='))
    .ToDictionary(s => s[0].Trim(), s => s[1].Trim());

Then display the game names in a ListBox : 然后在ListBox显示游戏名称:

listBox1.AddRange(
     _games.Keys
        .OrderBy(k => k)
        .ToArray()
);

Finally launch the selected game with 最终通过以下方式启动选定的游戏

string gamePath = _games[listBox1.SelectedItem];
var processStartInfo = new ProcessStartInfo(gamePath);
processStartInfo.WorkingDirectory = Path.GetDirectoryName(gamePath);
Process.Start(processStartInfo); 

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

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