You can use AppDomain.CurrentDomain.BaseDirectory
.
Capture the directory path it uses to run the program
Question
I have a C# application which assume it runs from bin directory
string current_directory = Directory.GetCurrentDirectory(); //get current directory, this is the bin dir
string parent_dir = Directory.GetParent(current_directory).ToString();// this is parent of bin dir
string _Config1 = parent_dir + "\\config\\x.cfg";
string _Config2 = parent_dir + "\\config\\y.cfg";
string _Log = parent_dir + "\\log\\z.log";
The problem is for some reasons the user can not go to the bin directory and run the app (just type "application_name"). He has to run it using path (ie d:\blah\1\blah\2\blah\3\bin\application_name
)
when he does this he gets
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
so either I have to capture the path he uses to run the program and use it in my program or somehow make my application to be able to run using path.
Solution
OTHER TIPS
Use the Application.StartupPath
for WinForms
.
You can use reflection:
var path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().GetCodebase );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow