none
How to get current directory path in C#. RRS feed

  • Question

  • 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

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

    Friday, September 14, 2018 5:40 AM

All replies

  • Try this:

    var currentDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
    Friday, September 14, 2018 5:48 AM
  • Hi,
    Thank you for quick response.

    it throws following exception :
    The path is not of a legal form.
       at System.IO.Path.LegacyNormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
       at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
       at System.IO.Path.InternalGetDirectoryName(String path)
    Friday, September 14, 2018 6:02 AM
  • Hi, what is the result of Assembly.GetCallingAssembly().Location ? without Path.GetDirectoryName()

    There is also a method from System.IO

    Directory.GetCurrentDirectory()


    Friday, September 14, 2018 6:12 AM
  • Hi,

    I've already tried Directory.GetCurrentDirectory(),
    it returns "
    C:\Windows\SysWOW64"even when installer folder is on desktop or any other drive.

    what is the result of Assembly.GetCallingAssembly().Location ? without Path.GetDirectoryName()
    I'll try it.

    Thanks.

    Friday, September 14, 2018 6:43 AM
  • Hi,

    I just tried Assembly.GetCallingAssembly().Location, it returns empty string.

    Thanks.

    Friday, September 14, 2018 6:51 AM
  • Maybe you can try other methods like :

    var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;

    Friday, September 14, 2018 7:42 AM
  • So you have enhanced InstallShield (custom action) with your own library code?

    Take a look at the InstallShield doc in this case. You need to pass this as custom action data. Or you look at the MSI interop library.

    Friday, September 14, 2018 7:59 AM
  • Hi Stefan,

    Thank you for the response.

    I'm not exactly enhancing existing actions. I've just added a couple of form in installation process that uses my own library code as APIs for getting necessary values.

    What I wanted to do was instead of passing installer execution path from installer I wanted my library to handle it, but now I feel it's impossible.

    I'll take a look at the link you provided in order to understand how install shield works.

    Thank you.

    Friday, September 14, 2018 8:15 AM
  • Hi VishwasTrivedi,

    It seems that you could save the installation path into registry then you could retrieve the path from registry. 

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 17, 2018 5:18 AM
    Moderator