部署dll后如何获取C#中的安装目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/333767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:35:28  来源:igfitidea点击:

How to get the installation directory in C# after deploying dll's

c#deploymentinterop

提问by Presidenten

Is there some smart way to retreive the installation path when working within a dll (C#) which will be called from an application in a different folder?

在从不同文件夹中的应用程序调用的 dll (C#) 中工作时,是否有一些聪明的方法来检索安装路径?

I'm developing an add-in for an application. My add-in is written in C#. The application that will use is written in C and needs to compile some stuff during evaluation, so I have a middlestep with a C++ dll that handles the interop business with C# and only shows a clean interface outward that C can work with.

我正在为应用程序开发加载项。我的加载项是用 C# 编写的。将使用的应用程序是用 C 编写的,需要在评估期间编译一些东西,所以我有一个 C++ dll 的中间步骤,它处理与 C# 的互操作业务,并且只显示一个干净的外部接口,C 可以使用。

What I deploy will be a set of .dll's and a .lib and .h for the C++ part (sometimes static binding will be necessary).

我部署的将是一组 .dll 以及 C++ 部分的 .lib 和 .h(有时需要静态绑定)。

When trying out the setup and printing out the current directory info from the C# dll with:

在尝试设置并从 C# dll 打印出当前目录信息时:

        Console.WriteLine(Directory.GetCurrentDirectory());

or:

或者:

        Console.WriteLine(System.Environment.CurrentDirectory);

I get the executables path.

我得到了可执行文件路径。

So ... once again, how do I get the installation path of my dll?

所以...再次,我如何获得我的dll的安装路径?

Edit: They both worked! Thanks for the quick reply guys!

编辑:他们都工作了!感谢您的快速回复!

采纳答案by tvanfosson

I think what you want is Assembly.GetExecutingAssembly().Location.

我想你想要的是Assembly.GetExecutingAssembly().Location

回答by Cristian Libardo

Try this:

尝试这个:

typeof(TypeInMyModule).Assembly.Location

回答by Sonhja

One of these two ways:

这两种方式之一:

using System.IO;
using System.Windows.Forms;
string appPath = Path.GetDirectoryName(Application.ExecutablePath);

Or:

或者:

using System.IO;
using System.Reflection;
string path = Path.GetDirectoryName(
Assembly.GetAssembly(typeof(MyClass)).CodeBase);