C# How to get current directory path

Viewed 19625

Scenario : I've created a library project which provides interface to installer(exe created using install shield).I want to check whether a particular file exists in the installer folder.

I've tried following :

1).  Assembly.GetEntryAssembly().Location

// Throws "Object reference not set to an instance of an object"

2). new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath

// return : C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll

3). Assembly.GetExecutingAssembly().Location

// returns empty string

4). AppDomain.CurrentDomain.BaseDirectory

// returns "C:\Windows\syswow64\" irrespective of the actual path

Could anyone tell me how can I get directory path from where the installer is being executed?

7 Answers

You can use

Directory.GetCurrentDirectory();

to get current directory path

import System.IO

I will answer my own question, It's not possible to get current path from library project. You'll have to get it from installer. There is no other way.

Thank you.

If you want to get the folder where your exe is resting

AppDomain.CurrentDomain.BaseDirectory

this returns the whole path to the Folder which contains the exe. From there on you can add your folders manually

AppDomain.CurrentDomain.BaseDirectory + @"MyFolder1\MyFolder2";

Assuming you are running an MSI-based install built by InstallShield:

The problem you're having is because of the way those calls work. You're implementing this in an external Dll, and Dlls don't have their own current directory - they have the current directory of the process that's calling into the Dll. In this case you're being called from an msiexec.exe process (assuming you are doing this in a custom action) so you'll get msiexec.exe's current directory.

Apart from that, some other observations:

  1. You don't say exactly when you are calling your code, but it might be before the user has chosen the installation folder, so the search doesn't really help.

  2. InstallShield probably provides support for a file search. If this is an MSI setup it definitely does.

  3. Installs hardly ever look for files in an install folder because there is rarely a good reason. If you need the file, add it to your install, or if it's a redistributable then add the standard redist package, maybe a merge module. If that file belongs to another setup, what will you do if that product is uninstalled? Are you checking the version to see if it's compatible with your application? In general, all setups install the files they need and shared files are backwards compatible if they are used by several different products.

Try this one, it always works for me:

Environment.CurrentDirectory

use above property as follow.

string currentDirectory = Path.GetDirectoryName(Environment.CurrentDirectory);

If you want that path in the InstallShield use this

Setup.exe - Use the SETUPEXEDIR property

http://www.installshield.com/news/newsletter/0206-articles/SetupExeDir.asp.

Depending on version of installshield you are using try different properties

1. SRCDIR

2. SETUPEXEDIR

3. SUPPORTDIR

If you have a C# code to read preperties use MsiGetProperty like below

MsiGetProperty( hMSI, "SETUPEXEDIR", Value, Size )

string assemblyPath = Context.Parameters["assemblyPath"];
int i = assemblyPath.Length - 1;
while (assemblyPath[i] != '\\') --i;
string folder = assemblyPath.Substring(0, i);
Related