0

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 ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

3

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); 
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • OK This is my code: namespace GameLauncher { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { string executableDir = Path.GetDirectoryName(Application.ExecutablePath); Process.Start(Path.Combine(executableDir, @"F:\Freak Mind Games\Projects 2013\JustShoot\game.bat")); } } } and this are the two errors I've got: https://dl.dropboxusercontent.com/u/42689244/error1.JPG https://dl.dropboxusercontent.com/u/42689244/error2.JPG –  Apr 16 '13 at 19:57
  • The variable `executableDir` contains the directory already. Therefore drop the directory part of your path! `Path.Combine(executableDir, "game.bat")`. Probably `executableDir` contains: `"F:\Freak Mind Games\Projects 2013\JustShoot"`. – Olivier Jacot-Descombes Apr 16 '13 at 20:04
  • sorry. I don't understand. Where do I have to place my path F:\Freak Mind Games\Projects 2013\JustShoot\ ? –  Apr 16 '13 at 20:06
  • Nowhere! `Path.GetDirectoryName(Application.ExecutablePath)` gives you the directory of `justshoot.exe`. – Olivier Jacot-Descombes Apr 16 '13 at 20:09
  • But I don't want to place it in the folder :P Now, when I place it in the folder of Just Shoot it runs but I don't wanna do this :P –  Apr 16 '13 at 20:12
  • Where do you want to place "game.bat" in relation to your game launcher (I assume its "justshoot.exe") then? – Olivier Jacot-Descombes Apr 16 '13 at 20:20
  • This should be not a launcher for only this game ! this should become a launcher for multiple games –  Apr 16 '13 at 20:23
  • var processStartInfo = new ProcessStartInfo(@"F:\Freak Mind Games\Projects 2013\JustShoot\JustShootLauncher.exe"); processStartInfo.WorkingDirectory = Path.GetDirectoryName(@"F:\Freak Mind Games\Projects 2013\JustShoot\JustShootLauncher.exe"); Process.Start(processStartInfo); –  Apr 16 '13 at 20:41
  • I added an update explaining a possible solution where the launchable games are configured in a text file. (Note: I have not tested the code.) – Olivier Jacot-Descombes Apr 16 '13 at 21:36
  • Awww. Where do I have to put this in ? Form_load ? I've got an issue with .AddRange :( –  Apr 16 '13 at 21:52
  • Ok fixed the other stuff. This is my only issue right now :P _games[listBox1.SelectedItem]; Can't convert object in string –  Apr 16 '13 at 22:00
  • Can we replace the listBox with a listView ? Then I can add name, publisher and a background to it :) –  Apr 17 '13 at 00:10
  • Of course you can. I'm not very used to ListViews, they are bit more complicated to handle. You have to define the headers and to add sub-items. See [C#: How to add subitems in ListView](http://stackoverflow.com/q/729090/880990) – Olivier Jacot-Descombes Apr 17 '13 at 11:44