Dafna
Dafna

Reputation: 181

Calling a OpenFileDialog from a custom action after clicking a button

I would like to be able to choose a file (and not a directory) from within a dialog.

For that I have created a custom action that calls the 'OpenFileDialog'. I am calling the custom action from a button control and it seems as if it gets totally stuck (I have to kill the msi process in order to continue)

Here is my custom action:

[CustomAction]
        public static ActionResult BrowseForLicenseFile(Session session)
        {
            try
            {
                session.Log("Begin BrowseForLicenseFile");

                session["LICENSEFILE"] = string.Empty;

                var dialog = new OpenFileDialog
                {
                    Filter = "License File (license.dat)|license.dat",
                    InitialDirectory = @"C:\",
                    Title = "Select License File: license.dat"
                };

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    session["LICENSEFILE"] = dialog.FileName;
                    session.Log("setting property LICENSEFILE={0}", dialog.FileName);
                }
                else
                    return ActionResult.Failure;
            }
            catch (Exception ex)
            {
                session.Log("ERROR in custom action BrowseForLicenseFile {0}", ex.ToString());
                return ActionResult.Failure;
            }

            return ActionResult.Success;
        }

Button code (I even tried commenting two last 'Publish' lines and leaving only the first one:

 <Control Id="BrowseLicenseFile" Type="PushButton" Width="75" Height="17" X="274" Y="105" Text="{\VSI_MS_Sans_Serif13.0_0_0}Browse">
          <Publish Event="DoAction" Value="BrowseForLicenseFileCA" Order="1" >1</Publish>
          <Publish Property="LICENSEFILE" Value="[LICENSEFILE]" Order="2">1</Publish>
          <Publish Event="DoAction" Value="CopyLicenseFileToCommonDirCA" Order="3">1</Publish>
        </Control>

Here is how the custom actions are declared:

    <CustomAction Id="BrowseForLicenseFileCA" BinaryKey="ServerInstallerCustomActions.CA" DllEntry="BrowseForLicenseFile" Execute="immediate" Return="check" />
    <CustomAction Id="CopyLicenseFileToCommonDirCA" BinaryKey="ServerInstallerCustomActions.CA" DllEntry="CopyLicenseFileToCommonDir" Execute="immediate" Return="check" />
 <Binary Id="ServerInstallerCustomActions.CA" SourceFile="$(var.Server.TargetDir)\ServerInstallerCustomActions.CA.dll" />

The property is declared as empty:

 <Property Id="LICENSEFILE" />

I am running the msi from a command prompt with administrative privileges.

Would appreciate if someone could figure out why this is not working.

Upvotes: 0

Views: 1139

Answers (1)

Dafna
Dafna

Reputation: 181

I am putting the answer for others to use:

 [CustomAction]
            public static ActionResult BrowseForLicenseFile(Session session)
            {
                try
                {
                    Log(session, "Begin BrowseForLicenseFile");

                    session["LICENSEFILE"] = string.Empty;

                    var task = new Thread(() => GetFile(session));
                    task.SetApartmentState(ApartmentState.STA);
                    task.Start();
                    task.Join();
                    Log(session, "End OpenFileChooser Custom Action");

                }
                catch (Exception ex)
                {
                    Log(session, "ERROR in custom action BrowseForLicenseFile {0}", ex.ToString());

                    return ActionResult.Failure;
                }

                return ActionResult.Success;
            }
    private static void GetFile(Session session)
    {
        var dialog = new OpenFileDialog
        {
            Filter = "License File (license.dat)|license.dat",
            InitialDirectory = @"C:\",
            Title = "Select License File: license.dat"
        };

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            session["LICENSEFILE"] = dialog.FileName;
            Log(session, "setting property LICENSEFILE={0}", dialog.FileName);
        }
    }

Upvotes: 1

Related Questions