2

I'm trying to execute a tool-exe which is located in the same directory as main exe. For this I'm trying to obtain first the exe-name of the process using Assembly.GetExecutingAssembly and then getting the directory using IO.Path.GetDirectoryName

//1
String exePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; 
//2
String ncpath = System.IO.Path.GetDirectoryName(exePath);

1 returns "file:///C:/Development/RC_trunk/bin/Release/ResultConfirmation.EXE" It is an URI. Not exactly what I need but ok.

2 returns "file:\C:\Development\RC_trunk\bin\Release" Which seems to be a simple [/]+ to \ replacement.

Am I using wrong API for my problem?

P.S. IDE is VS2008

Valentin H
  • 7,240
  • 12
  • 61
  • 111

3 Answers3

2

You need to use GetExecutingAssembly.Location:

System.Reflection.Assembly.GetExecutingAssembly().Location

instead of CodeBase and then use GetDirectoryName.

keenthinker
  • 7,645
  • 2
  • 35
  • 45
2

To get the application directory, try AppDomain.CurrentDomain.BaseDirectory.

Take a look at Best way to get application folder path for other ways of accessing the directory.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131
  • OK, looks like there are a few possibilities for selecting a wrong one for a newcomer. Thanks for the link! – Valentin H Nov 03 '13 at 22:33
1

Use this instead for step 1)

string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
helb
  • 7,609
  • 8
  • 36
  • 58