Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 817
Presidenten Avatar asked Dec 02 '08 12:12

Presidenten


People also ask

What is the install directory of a program?

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.

How do I find the installation directory in a script?

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:

Where can I find the installation location of a program on Windows?

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

How to get the current working directory in C?

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.


3 Answers

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

like image 52
tvanfosson Avatar answered Sep 21 '22 00:09

tvanfosson


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);
like image 20
Sonhja Avatar answered Sep 17 '22 00:09

Sonhja


Try this:

typeof(TypeInMyModule).Assembly.Location
like image 27
Cristian Libardo Avatar answered Sep 21 '22 00:09

Cristian Libardo