开发者

How to get application path

i am using

string path开发者_如何学C = AppDomain.CurrentDomain.BaseDirectory; to get my application path ,but this gives something like

C:\Projects\XYZ\ABC\bin\Debug

i don't want bin\Debug .Is there any way to achieve this ?


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.


to get what you want:

var enviroment = System.Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(enviroment).Parent.FullName;


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

string path = Application.ExecutablePath;


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", "");


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.


I am using this:

String appSettingsPath = Directory.GetCurrentDirectory();

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜