How to get the execution folder if the file executed is in another folder?
-
Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted
- Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)
-
Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted
- Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)
found it, this does the trick:
Directory.GetCurrentDirectory();
- Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)
-
Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted
- Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)
If I have understood your requirement correctly you could create an environment variable to give you accees to your application form anywhere. On the Advanced tab of System Properties click on the Environment Variables button. Add the path to your app to the PATH variable and you will be able to start it from anywhere.
me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven
-
Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted
- Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)
You can pick up the location of many different "special" folders. This will get you the Program Files folder:
string strProgFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
If, however, you really meant that you wanted the name of the directory where the assembly was located, then you should use:
using System.IO;
.
.
.string strMyHomeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
As an aside, rather than adding a program to the Windows folder, you should create a folder elsewhere (I have one called Tools) and add that folder to your environment Path. Perhaps it's just me but I don't like cluttering up system file locations.