How to get application path

给你一囗甜甜゛ 提交于 2019-11-29 10:51:32

The AppDomain.CurrentDomain.BaseDirectory property gets the base directory that the assembly resolver uses to probe for assemblies.

So it's functioning 100% as it should. If you were to build your application, cut and paste it somewhere else in another folder or drive. Those changes would be reflected in this property.

Also, you mentioned that you do not want this part bin\Debug, so you want what's before that? Please be specific.

If you want to figure out your application executable path (as I understood):

string path = Application.ExecutablePath;

You are running the program within IDE, and that's the reason why you get this kind of path. Try to build the app and run it outside IDE - you'll see that the method works correctly.

Edit: what you obtain is because IDE runs debug build of your app which is located in $PROJECT_DIR\bin\Debug.

string LPath;
string Location = AppDomain.CurrentDomain.BaseDirectory + "Reports\\rptEmployInfoStat.rpt";
int index;
index = Location.IndexOf("bin");
if (index > 0)
{
     LPath = Location.Remove(index, 10);
}
else
{
     LPath = Location;
}
rd.Load(@LPath);

To be honest, this is not the best pratice but this will give what you want:

string dir = System.IO.Directory.GetCurrentDirectory().Replace("\\bin\\Debug", "");

I am using this:

String appSettingsPath = Directory.GetCurrentDirectory();

        if (!File.Exists(Path.Combine(appSettingsPath, "appsettings.json")))
            appSettingsPath = Path.GetDirectoryName(Path.GetDirectoryName(appSettingsPath));

to get what you want:

var enviroment = System.Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(enviroment).Parent.FullName;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!