C# - Path to execution

Asked

Viewed 1,930 times

4

I am making a Auncher, however I would like to leave the files with a "fixed" path, the Auncher is in the same folder of the file to be started, but I do not know how I could create a "middle way", something like in html, that just put a piece of the way, if it is in the same folder, as is the case.

I’m using forms. NET 3.5.

The folders are like this:
folder x/folder Auncher/...
folder x/folder Prog/...

  • In Launcher Use ".. /folder Prog"

  • @Tony, tried .. /folder Prog/ and .. folder Prog, did not work.

  • @lsalamon, I know which is the way of . exe, the problem is that I’m not getting to do the relative way.

  • It would make sense to @"folder x/folder Launcher/folder Launcher/bin/Debug/folder Prog"?

2 answers

2


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"));
  • I tried all the methods by placing a Process.Start(programPath); at the end, but they didn’t work, said that I couldn’t locate the file.

  • @Patrick What is the complete path to Auncher and the program to run?

  • Launcher: C:\Users\Patrick\Desktop\programas\LAUNCHER\LAUNCHER\bin\Debug\LAUNCHER.exe Programa: C:\Users\Patrick\Desktop\programas\programa1\programa1\bin\Debug\programa1.exe

  • @Then the path will have many more "..". Try ../../../../../programa1/programa1/bin/Debug/programa1.exe

  • I used the last method that showed, it worked, but what if the amount of folders varies until C:? If you put yourself inside other folders, how does it look?

  • If the amount of folders varies a lot, then it might be better not to use relative paths and use absolute paths instead. You can have another file in the Launcher folder that indicates the path to the script and read ".. /path.txt" to know which file to run.

  • Specifically, to have the folder Auncher is in, you use .. (and not just .). Then, for each folder to go up, add /...

  • If I burn this on a CD it would be, then, ..\..\programa1\programa1\bin\Debug\programa1.exe, which in case would be F:(ou qualquer outra unidade)\programas\programa1\programa1\bin\Debug\programa1.exe?

  • No. You use the ".." for go up, not to go down. Then it would still be ../../../../../programa1/programa1/bin/Debug/programa1.exe (assuming that Auncher is in F:\programas\LAUNCHER\LAUNCHER\bin\Debug\LAUNCHER.exe

  • No, but in this case, the program and Auncher would be in F:\programas\...&#And one thing I didn’t understand is / or ?

  • @Patrick Em Windows, / and \ will give the same (however, if using \ remember to escape with \\ or using @"" instead of "" . If Auncher is on F:\programas\LAUNCHER\LAUNCHER\bin\Debug\LAUNCHER.exe and the program in F:\programas\programa1\programa1\bin\Debug\programa1.exe so you need those .. all. The .. count from right to left.

  • Well, I moved everything to the E:\, now is E:\programas\LAUNCHER\... and E:\programas\programa1\...&#But when I put ..\..\programa1\... does not work, but when I put ..\..\..\..\..\programa1\..., works perfectly. Why, if there are only two folders, E:\programas\?

  • There are 4 folders: Debug, bin, LAUNCHER and LAUNCHER. Only in the fifth folder (programs) are they common. Hence they are 5 times ...

  • Ah, so he comes back, now I understand, finally.

Show 9 more comments

1

The name of this is not "fixed path" but yes "relative path"... but I think I understand what you mean, actually the string is that it’s fixed, representing a relative path. =)

I see no problem in using a relative path... just tested and works perfectly:

var proc = Process.Start(@"..\pasta prog\nomeDoPrograma.exe");

Attention when creating a shortcut for Auncher

Note that the relative path is solved in relation to the path indicated in Environment.CurrentDirectory. If you create a shortcut for Launcher, the property that indicates the start path will be passed to this environment variable . Net.

Tela de propriedades do atalho

  • I am new in this area and I could not understand very well this code... Could pass the full context?

  • You could put this command inside any method of your code... say, the event click a button written "Launch Application".

  • In Visual Studio, you can put a button in the Form and give two clicks on it that goes straight to the click event, then you can put the command I indicated in this event.

  • That’s what I did, but gave Exception, says it could not find the specified file.

  • You will need to check the variable Environment.CurrentDirectory... and see if the relative path actually contains the executable you are calling.

  • But do you have to check even if the file is really there? 3

  • Has, if the file is not there has no way to run.

  • And how could I do it?

  • You can check via code like this: if (File.Exists(@"..\pasta prog\nomeDoPrograma.exe"))

  • Well, I used if (File.Exists(@"..\pasta x\pasta prog\prog.exe") {Process.Start(@"..\pasta x\pasta prog\prog.exe")}, but nothing happened, nor did Exception give, as if the button had no function.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.