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

*爱你&永不变心* 提交于 2019-12-05 18:39:40

问题


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?

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.

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).

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

        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?

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


回答1:


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




回答2:


Try this:

typeof(TypeInMyModule).Assembly.Location



回答3:


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);


来源:https://stackoverflow.com/questions/333767/how-to-get-the-installation-directory-in-c-sharp-after-deploying-dlls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!