问题
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