C# How to get current directory path

17,352

Solution 1

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.

Solution 2

You can use

Directory.GetCurrentDirectory();

to get current directory path

import System.IO

Solution 3

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.

Solution 4

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";
Share:
17,352
JustAProgrammer
Author by

JustAProgrammer

Updated on June 12, 2022

Comments

  • JustAProgrammer
    JustAProgrammer almost 2 years

    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?

  • JustAProgrammer
    JustAProgrammer over 5 years
    AppDomain.CurrentDomain.BaseDirectory returns "C:\Windows\syswow64\" path and installer is running from desktop.
  • JustAProgrammer
    JustAProgrammer over 5 years
    Directory.GetCurrentDirectory() return "C:\Windows\SysWOW64" path and my installer is running from desktop.
  • JustAProgrammer
    JustAProgrammer over 5 years
    it return "C:\Windows\SysWOW64" path even when installer is in another directory.
  • ksdev
    ksdev over 5 years
    I think, there is issue of application release. Verify build of project property. May be you will get that.
  • JustAProgrammer
    JustAProgrammer over 5 years
    Sorry, I don't quit understand. Are you saying there are any settings in VS build settings?
  • JustAProgrammer
    JustAProgrammer over 5 years
    I can't use Path.GetDirectoryName(Application.ExecutablePath), I have a Class Library project and Application class is in System.Windows.Forms namespace.
  • ksdev
    ksdev over 5 years
    Yeas. But which kind of application you are develop now? For build verification, right click on project and open property there is build.
  • JustAProgrammer
    JustAProgrammer over 5 years
    I'm building a class library. Which settings are you referring to in Build?
  • ksdev
    ksdev over 5 years
    I have tested in class library project. I got correct path of working directory by string currentDirectory = Path.GetDirectoryName(Environment.CurrentDirectory); If this is not work then i think there is bug.
  • JustAProgrammer
    JustAProgrammer over 5 years
    Wouldn't this be equal to passing path from Installer to dll?
  • Pramod Tiwari
    Pramod Tiwari over 5 years
    try to look of InstallShield document .may be some custom type property
  • ManishM
    ManishM over 5 years
    InstallShield keeps the default properties itself. You can use it in the actions. You can also inject C# code for any big logical processing. There you need MsiGetProperty to access the properties. I recommend you to go through InstallShield documentation and try something.