Reputation: 2509
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"
Upvotes: 0
Views: 121
Reputation: 13707
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.
Upvotes: 0
Reputation: 10776
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:
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...
Upvotes: 1
Reputation: 1062745
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"
Upvotes: 1