In C#, it is possible get path to the program executable to be executed.
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
In which Assembly
is in the namespace System.Reflection
.
This gives us the path to the executable of Auncher. But this is not what is intended.
If the Auncher is "C:/folder x/folder Launcher/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 have the path to the program to run in the variable programPath
.
But there is a simpler way to get relative paths. For this, let’s 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"));
In Launcher Use ".. /folder Prog"
– Tony
@Tony, tried .. /folder Prog/ and .. folder Prog, did not work.
– ptkato
See if it helps: How to take the path of the open executable in C#
– lsalamon
@lsalamon, I know which is the way of . exe, the problem is that I’m not getting to do the relative way.
– ptkato
It would make sense to @"folder x/folder Launcher/folder Launcher/bin/Debug/folder Prog"?
– ptkato