0

I just want to Code an Launcher for the Game i currently making, but i ran into an Problem and i am an absolutely beginner as a Programmer.

Currently i have a Start Button with looks like this

private void startbtn_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("C:\\Users\\Windows\\Desktop\\Folder\\Underfolder\\Game.exe");
}

But i try to make it Dynamic, so the Launcher can work which ever the User install the Data (There is an Installer for everything) also the launcher is located in the same directory as the Game.exe

(The Code looks weird on the Post but it is correct)

d219
  • 2,707
  • 5
  • 31
  • 36
  • 2
    Possible duplicate of [Getting the absolute path of the executable, using C#?](https://stackoverflow.com/questions/1658518/getting-the-absolute-path-of-the-executable-using-c) – Rufus L Mar 29 '18 at 23:06

2 Answers2

1

The most reliable way I've found to do this is (assuming Game.exe is located in the same path as your Launcher.exe as you mentioned in your post):

var launcherExeDirectory = AppDomain.CurrentDomain.BaseDirectory;
var gameExeFullPath = Path.Combine(launcherExeDirectory, "Game.exe");

Then you can just do something like:

Process.Start(gameExeFullPath);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

Just use GetCurrentDirectory

Process.Start(System.IO.Directory.GetCurrentDirectory + @"\game.exe");

///Or

Process.Start(AppDomain.CurrentDomain.BaseDirectory + @"\game.exe");

///or

Process.Start(Application.ResourceAssembly.Location + @"\game.exe");

///or

Process.Start( System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\game.exe");
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • You need to escape that backslash. Also, I'd recommend using `Path.Combine()` which handles that for you automatically. – itsme86 Mar 29 '18 at 23:06
  • @itsme86,looks ok now ? – Software Dev Mar 29 '18 at 23:07
  • @RufusL,added alternatives , OP can apply any :) ... But OP seems inactive :( – Software Dev Mar 29 '18 at 23:11
  • @zack raiyan, i try this. Would this be the entire Code for the Button or is there something i have too add? (Sorry for the many edits, this site is confusing me a little bit at the moment ^^") – Darth Revan Mar 31 '18 at 13:06
  • U don't need to add anything , and if this answer helps you,please mark it as the answer – Software Dev Mar 31 '18 at 13:08
  • didn't the answer work ?? why did u uncheck it as the answer ? – Software Dev Mar 31 '18 at 13:14
  • The answer might work, but there is an error. i cant use an +- Operator on a string. I would send an Screenshot, but its on German. – Darth Revan Apr 01 '18 at 17:46
  • System.Diagnostics.Process.Start(System.IO.Directory.GetCurrentDirectory + @"\game.exe"()); With the both Brackets at the End it seems to run. But i cant release the Project to get the .exe to test it final because he wants Game.exe which isnt in the Projects Folder. How can i avoid this now? – Darth Revan Apr 01 '18 at 18:55
  • If game.exe is not in the same folder as the project,just provide the folder path of the .exe ,isn't it obvious ? – Software Dev Apr 01 '18 at 18:57
  • Afterwards the Game .exe will be in the same directory. But firsat i have to create the .exe before i can do that. – Darth Revan Apr 01 '18 at 19:02
  • Or do i missunderstand the error? C# Method name expected – Darth Revan Apr 01 '18 at 19:02
  • How should i explain this? The exe already exits, but not in the \repos\launcher Visual Studio Created. I cant create the Debug or Release Version because he cant find. But if i change the Path to it where it is now, it is afterwards Wrong when i put the Created exe into the final Folder. Its hard to explain, Hope you can understand – Darth Revan Apr 01 '18 at 19:13
  • [Let's continue this discussion in chat](https://chat.stackoverflow.com/rooms/168004/students) – Software Dev Apr 01 '18 at 19:16
  • i don't understand ? u can always mark the answer as answer which u already did ... anyway,join me in the chat stated above – Software Dev Apr 01 '18 at 19:22