Reputation:
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 ?
Upvotes: 0
Views: 391
Reputation: 112259
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, i.e.
F:\Freak Mind Games\Projects 2013\JustShoot\justshoot.exe
.
Path.GetDirectoryName(...)
gets the directory part of it, i.e.
F:\Freak Mind Games\Projects 2013\JustShoot
.
Path.Combine(executableDir, "game.bat")
combines the directory with game.bat
, i.e. 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"
. 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 (e.g. "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
:
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);
Upvotes: 3