In C #, you can get the path to the program executable to be executed.
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
Where Assembly
is in the namespace System.Reflection
.
This gives us the path to the launcher executable . But this is not what is intended.
If the launcher is "C: / x folder / launcher folder / launcher.exe", we want to have the path to "C: / folder x / folder prog /".
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string launcherPath = Uri.UnescapeDataString(uri.Path); //launcher.exe
string launcherDir = Path.GetDirectoryName(launcherPath); //pasta launcher
string appDir = Path.GetDirectoryName(launcherDir); //pasta x
string programPath = Path.Combine(appDir, "pasta prog", "program.exe");
Path
belongs to the namespace System.IO
.
With this code, we get the path to the program to run in the programPath
variable.
But there is a simpler way to get relative paths. For this, we will combine the methods Path.Combine
and Path.GetFullPath
.
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string launcherPath = Uri.UnescapeDataString(uri.Path);
string programPath = Path.GetFullPath(Path.Combine(launcherPath, "../../pasta prog/program.exe"));