i was able to create a shortcut name dynamically, now i need to link it to executable of my software. to do that i need to know where the software has been installed.

So the question is how to get installation folder in custom installer class?

Thanks.

Dani AI

Generated

Brief addendum and best practice for future readers.

When the code runs inside a custom Installer (the Install/Commit methods), rely on the Installer context rather than Application.ExecutablePath. The usual pattern is to pass the installer folder into the custom action via the setup project's CustomActionData and read that value from Context.Parameters. That makes the install path explicit (and works regardless of how the MSI launches the custom action). If the custom-action value is missing, fall back to the folder of the running assembly.

Example fallback pattern:

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    string installFolder = Context.Parameters["targetdir"];
    if (string.IsNullOrEmpty(installFolder))
    {
        string asmPath = Assembly.GetExecutingAssembly().Location;
        installFolder = Path.GetDirectoryName(asmPath);
    }

    Context.LogMessage("Install folder: " + installFolder);
    // create shortcut or use installFolder here
}

Troubleshooting and cautions:

  • Ensure the CustomActionData string in the setup project actually passes the MSI property you need (common names are TARGETDIR or INSTALLDIR depending on the installer). If Context.Parameters["targetdir"] is null, the CustomActionData was not set or the property name is different.
  • Normalize the path (remove extra quotes or trailing backslashes) before using it to create shortcuts.
  • Avoid using Assembly.CodeBase raw (it can be a file:// URI). Prefer Assembly.Location or convert the CodeBase with new Uri(codeBase).LocalPath.
  • For runtime shortcuts (after the app is installed and running), the executable folder can be found from the assembly or application API; for installer-time logic use the Context-based approach above.

This ties together 's runtime suggestions and 's pointer to Installer.Context while adding practical setup and debugging tips.

Recommended Answers

All 3 Replies

Hi serkan sendur,

To get only the folder part of the path, use static method GetDirectoryName of Path class.

using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

To get assembly in which the specified class is defined use method Assembly.GetAs­sembly (with the specified class type as a paramater). The assembly must be loaded. Next get assembly file path using Assembly.CodeBase property.

using System.IO;
using System.Reflection;

string path = Path.GetDirectoryName(
                     Assembly.GetAssembly(typeof(MyClass)).CodeBase);

I hope its helps

You can get this information from Installer.Context Property.

Check this link:

commented: good information +4

ok this problem is solved, i attach the example project to this post.
i was able to get the installation folder from within the custom installer class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.