문제

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().Lo‌​cation)

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.

도움이 되었습니까?

해결책

You can use AppDomain.CurrentDomain.BaseDirectory.

다른 팁

Use the Application.StartupPath for WinForms.

You can use reflection:

var path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().GetCodebase );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top