如何获取应用程序路径

发布于 2024-10-31 11:59:15 字数 204 浏览 3 评论 0原文

我正在使用

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

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

我不想要 bin\Debug 的信息。有什么方法可以实现此目的吗?

i am using

string path = 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ˇ宁静的妩媚 2024-11-07 11:59:15

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

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

另外,您提到您不想要这部分 bin\Debug,所以您想要之前的部分?请具体说明。

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.

一杆小烟枪 2024-11-07 11:59:15

得到你想要的:

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

to get what you want:

var enviroment = System.Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(enviroment).Parent.FullName;
你列表最软的妹 2024-11-07 11:59:15

如果您想找出应用程序可执行路径(据我所知):

string path = Application.ExecutablePath;

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

string path = Application.ExecutablePath;
浅沫记忆 2024-11-07 11:59:15
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 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);
夜夜流光相皎洁 2024-11-07 11:59:15

老实说,这不是最好的做法,但这将提供您想要的:

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

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", "");
記柔刀 2024-11-07 11:59:15

您正在 IDE 中运行该程序,这就是您获得这种路径的原因。尝试构建应用程序并在 IDE 外部运行它 - 您会发现该方法工作正常。

编辑:您获得的是因为 IDE 运行位于 $PROJECT_DIR\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.

指尖上的星空 2024-11-07 11:59:15

我正在使用这个:

String appSettingsPath = Directory.GetCurrentDirectory();

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

I am using this:

String appSettingsPath = Directory.GetCurrentDirectory();

        if (!File.Exists(Path.Combine(appSettingsPath, "appsettings.json")))
            appSettingsPath = Path.GetDirectoryName(Path.GetDirectoryName(appSettingsPath));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文