如何获得应用程序路径

我在用

string path = AppDomain.CurrentDomain.BaseDirectory; 获取我的应用程序路径,但这给出了类似的东西

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

我不想bin \ Debug。有没有办法实现这个?

AppDomain.CurrentDomain.BaseDirectory属性获取程序集解析程序用于探测程序集的基目录。

所以它应该100%正常运作。 如果您要构建应用程序,请将其剪切并粘贴到其他文件夹或驱动器中的其他位置。 这些变化将反映在这个属性中。

另外,你提到你不希望这个部分bin\Debug ,所以你想要之前的那些? 请具体说明。

如果你想弄清楚你的应用程序可执行路径(据我所知):

 string path = Application.ExecutablePath; 

您正在IDE中运行该程序,这就是您获得此类路径的原因。 尝试构建应用程序并在IDE外部运行它 – 您将看到该方法正常工作。

编辑:您获得的是因为IDE运行您的应用程序的调试版本,该版本位于$ 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); 

说实话,这不是最好的实践,但这会给你想要的东西:

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

我用这个:

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