获取程序集可执行目录 [英] Get assembly executable directory

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

问题描述

我有两个应用程序,例如App1 和 App2.正常运行时,App1 会显示其程序集可执行位置.但是当我从 App2 调用 App1 时,它返回了 App2 的启动位置.

I have two applications, e.g. App1 and App2. When running normally, App1 will show its assembly executable location. But when I call App1 from App2, it returns App2's startup location.

那么,当我从 App2 调用 App1 时,如何获取 App1 的启动路径?

So, how do I get App1's startup path when I 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.

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

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