1

In my project \debug directory i have the program exe file for example:

test.exe

Now once i will run this test.exe from c:\ And in the second time i will copy the test.exe to d:\ and run it from there.

In my code i have this line:

string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + "\\Diagnostic Tool\\7z.dll";

Instead the program files x86 how can i get each the directory from where im running the exe file ?

2 Answers 2

1

One way (sure fire way also in .Net CE) is

string path = Path.GetDirectoryName(
  Assembly.GetEntryAssembly().GetModules()[0].FullyQualifiedName);

or

string path = Path.GetDirectoryName(
  Assembly.GetEntryAssembly().Location);

This will prevent Shortcut's from setting the applications CurrentDirectory or StartupPath which could technically be different from it's execution path (ClickOne programs for example).

0

You can get the running directory by doing:

Application.StartupPath

You can read more about it here

Alternatively you can try Environment.CurrentDirectory but that may not yield you the results you want because of short cuts and other ways of getting to your file.

http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx

You can also do:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

Or

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

Not the answer you're looking for? Browse other questions tagged or ask your own question.