Getting the SourceDir property from a C# custom ac

2019-08-03 03:13发布

问题:

I have some directories that are bundled with my installer and I need to access them from within a custom action. I have done some research and seen that the SourceDir can be used to obtain the currently executing dir location. However I cannot find any examples of how to obtain this property? Or another way to obtain the current directory?

Can anyone advise or point me to anything other than the unhelpful Microsoft site?

回答1:

I'm assuming you're using vbscript for the custom action. If so, properties can be accessed via the Session object. See below:

strSourceDir = Session.Property("SourceDir")

Be aware that the SourceDir property is only available at specific times during the installation.



回答2:

For C#, you'll find that you can do something like this:

[CustomAction]
public static ActionResult MyCustomAction(Session session)
{
    string sourceDir = session["SourceDir"];
    string path = Path.Combine(sourceDir, "yourfilename.txt");
    ...

The documentation on MSDN is unfortunately lacking in making this clear.

As w4g3n3r mentions in his answer, SourceDir is only available to you at certain times. In short, you will need to make sure your custom action is called after a call to the ResolveSource action, which can only be called after CostInitialize has run.

Once SourceDir is set, it should be available for use for the remainder of the installation process.



回答3:

Are you using InstallShield? Here's an example for an InstallScript CA:

MsiGetProperty(hMSI, "CustomActionData", strDirectory, numBuffer);

... where you also used a Set Property "Type 51" custom action to setup CustomActionData for your function to the value SOURCEDIR.