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!
In the build case, the install directory is chos When building or installing software, it means the directory tree where you want the application installed. in Windows, the default is C:\Program Files, but you could, if you have multiple disks, specify D:\Program Files or some other location.
So, instead of hard-coding a file system path, the best way to find an installation directory is to use the same technique that Windows uses. It doesn't change often and it is very unlikely to change without a version change that you can check in your script. If you need to use a file path or URL in a script, here are a few guidelines:
The program's installation directory is typically located somewhere within the Program Files folder on your system, but locating the correct folder can be time taking. Read on as we list the different ways you can use to find the install location of a program on Windows. 1. How to Find the Location of a Program Using the Start Menu
This article will explain several methods of how to get the current working directory in C. The getcwd function is a POSIX compliant system call that can retrieve the current working directory of the calling program. getcwd takes two arguments - char* buffer where the pathname is stored and the number of bytes allocated in the given buffer.
I think what you want is Assembly.GetExecutingAssembly().Location
.
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);
Try this:
typeof(TypeInMyModule).Assembly.Location
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With