string exeDirPath = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly())or for the full path of the executable:
string exePath = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;or just (only for Windows Forms application)
System.Windows.Forms.Application.ExecutablePath - Executable path of the application in which the assembly is housed:
string exePath = System.Windows.Forms.Application.ExecutablePath;or
string[] args = Environment.GetCommandLineArgs(); string exePath = args[0];To get the working directory where the executable lies (but this does not work for a service or so it seems):
string exePath = System.Windows.Forms.Application.StartupPath;or alternatively
string cwd = Environment.CurrentDirectory;Here is the sample output of these for a test application. The application was started in the "D:\Documents" directory but located at "D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug\IdeaTester.exe"
First the list of methods used:
1. System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString() 2. System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase 3. System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssem bly().GetName().CodeBase) 4. System.IO.Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetN ame().CodeBase).LocalPath) 5. System.IO.Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetN ame().CodeBase).AbsolutePath) 6. System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutableP ath) 7. System.IO.Path.GetDirectoryName(args[0]) 8. System.Windows.Forms.Application.StartupPath 9. Environment.CurrentDirectory 10. System.Windows.Forms.Application.ExecutablePathHere is the output of each method:
1. IdeaTester, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 2. file:///D:/Documents/Devel/Tools/IdeaTester/bin/x86/Debug/IdeaTester.exe 3. file:\D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug 4. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug 5. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug 6. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug 7. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug 8. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug 9. D:\Documents 10. D:\Documents\Devel\Tools\IdeaTester\bin\x86\Debug\IdeaTester.exe
No comments:
Post a Comment