tags:

views:

59

answers:

3

hi.

i have some files located in

c:\MyApp\file.txt

and current executing assenbly in in debud filder

c:\MyApp\bin\Debug\

i want to get address of above "file.txt" at run time..

i tried this

Assembly.GetExecutingAssembly().Location

it gives me c:\MyApp\bin\Debug\MyApp.exe

how can i get address of "file.txt"

+1  A: 

You can't, since the file and the compiled application have no relation. What if you moved the MyApp.exe somewhere else?

You have 2 options:

  1. Include file.txt in your project as a content file (it will be included in the output and in the same folder as MyApp.exe)
  2. Use configuration - allow a user option to define where to find the file. You can use AppSettings for this, for example.

Well, technically you can, since you can always navigate to the parent of the current directory ("MyAppPath\.."), but that's really just a bad idea...

Nader Shirazie
A: 

Your executable folder path can be retrieved with :

AppDomain.CurrentDomain.BaseDirectory

In order to get what you want you could do the following

string fileTxtPath = AppDomain.CurrentDomain.BaseDirectory + "..\\file.txt";

Of course if you have a file that is related to your project then you should include it in the same folder as your executable. Right click it from visual studio and go to properties, then select in the option Copy to Output Directory the Copy Always option.

Konstantinos
+1  A: 

The "Debug" folder (and the "Release" folder) are the actual output from your project's build. If you haven't included file.txt in the project and marked it as "Copy to Output Directory", then it doesn't count as part of the build output, and your app shouldn't try to load it.

Basically, just set "Copy to Output Directory" to "Copy always" or "Copy if newer", and continue to use the path "file.txt"

Marc Gravell