C#获取程序集可执行目录 [英] c# get assembly executable directory

查看:345
本文介绍了C#获取程序集可执行目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个申请。示例App1和App2
运行时,正常的App1将显示程序集可执行文件的位置。
但是当我从App2调用App1时,它将返回App2的启动位置。

I have 2 application. Example App1 and App2 When run nomal App1 will show assembly executable location. But when i call App1 from App2, it return App2 start up location.

那么,当从App2调用App1时如何获得App1的启动路径? / p>

So, how to get App1 start up path when call App1 from App2?

推荐答案

您可以使用以下命令获取当前正在执行的程序集的目录:

You can get the directory of the currently executing assembly with this:

string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

GetExecutingAssembly() 返回当前正在执行的程序集和 Location 是该程序集的完整路径或UNC路径。

GetExecutingAssembly() returns the currently executing assembly and Location is the full path or UNC path of that assembly.

Path.GetDirectoryName()返回完整路径的目录。

Path.GetDirectoryName() returns the directory of a full path.

请注意,程序集的路径与启动路径不同。启动路径是您从中启动应用程序的工作目录。而且,如果您的应用程序未更改其工作目录,则第一个应用程序启动的所有应用程序都将具有相同的启动路径。

Note that the assembly's path is not the same as the startup path. The startup path is the working directory from which you started an application. And if your app does not change it's working directory, all apps started by the first app will have the same startup path.

这篇关于C#获取程序集可执行目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆