Поделиться через


Walkthrough: Creating a Custom Action

The following walkthrough demonstrates the process of creating a DLL custom action to direct a user to a Web page at the end of an installation. You can use custom actions to run code after the installation finishes.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create the custom action

  1. On the File menu, click New Project.

  2. In the New Project dialog box, select Windows, and then click Class Library. In the Name box, type OpenWeb.

    The project is added to Solution Explorer.

  3. On the Project menu, click Add Class, and then in the Add New Item dialog box, select Installer Class. Accept the default name of Installer1, and then click Add.

  4. Switch to code view by clicking click here to switch to code view on the design surface (or by right-clicking the design surface and clicking View Code).

  5. In the Code Editor, add the following code (which opens a Web browser) to the Installer1 code file, under the constructor.

    <Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)>
    Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        MyBase.Install(stateSaver)
    End Sub
    
    <Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)>
    Public Overrides Sub Commit(
      ByVal savedState As System.Collections.IDictionary)
    
        MyBase.Commit(savedState)
        System.Diagnostics.Process.Start("https://www.microsoft.com")
    End Sub
    
    <Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)>
    Public Overrides Sub Rollback(ByVal savedState As System.Collections.IDictionary)
        MyBase.Rollback(savedState)
    End Sub
    
    <Security.Permissions.SecurityPermission(Security.Permissions.SecurityAction.Demand)>
    Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
        MyBase.Uninstall(savedState)
    End Sub
    
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);
    }
    
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
        System.Diagnostics.Process.Start("https://www.microsoft.com");
    }
    
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Rollback(IDictionary savedState)
    {
        base.Rollback(savedState);
    }
    
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
    }
    

    Note

    If you type Public Overrides and then type space, IntelliSense will provide a list of methods and properties; you can select Commit from the list and get the complete declaration. Repeat for the Install, Rollback, and Uninstall methods.

  6. In Solution Explorer, right-click Class1 code file, and then click Delete (because it is unnecessary).

To add a deployment project

  1. On the File menu, point to Add, and then click New Project.

  2. In the Add New Project dialog box, expand the Other Project Types node, expand the Setup and Deployment Projects, click Visual Studio Installer, and then click Setup Project. In the Name box, type Custom Action Installer.

    The project is added to Solution Explorer and the File System Editor is displayed.

  3. In the File System Editor, select Application Folder in the left pane. On the Action menu, point to Add, and then click Project Output.

  4. In the Add Project Output Group dialog box, OpenWeb will be displayed in the Project list. Select Primary Output.

    Primary Output from OpenWeb (Active) appears in the Application Folder.

To add the custom action

  1. Select the Custom Action Installer project in Solution Explorer. On the View menu, point to Editor, and then click Custom Actions.

    The Custom Actions Editor is displayed.

  2. In the Custom Actions Editor, select the Commit node. On the Action menu, click Add Custom Action.

  3. In the Select Item in Project dialog box, double-click the Application Folder. Select Primary output from OpenWeb.

    Primary output from OpenWeb appears under the Commit node in the Custom Actions Editor.

  4. In the Properties window, make sure that the InstallerClass property is set to True (this is the default).

  5. In the Custom Actions Editor, select the Install node and add Primary output from OpenWeb to this node as you did for the Commit node.

  6. On the Build menu, click Build Custom Action Installer.

To install on your development computer

  • Select the Custom Action Installer project in Solution Explorer. On the Project menu, click Install.

    This will run the installer and install Custom Action Installer on your development computer. At the end of installation, Internet Explorer should start and should open the Microsoft.com Web site.

    Note

    You must have install permissions on the computer in order to run the installer.

To deploy to another computer

  1. In Solution Explorer, right-click the setup project and click Open Folder in Windows Explorer.

  2. Navigate to the project output, and copy Custom Action Installer.msi, Setup.exe, and all other files and subdirectories in the directory to another computer.

    Note

    To install on a computer that is not on a network, copy the files to traditional media such as CD-ROM.

  3. On the target computer, double-click Setup.exe to run the installer.

    At the end of installation, Internet Explorer should start and should open the Microsoft.com Web site.

    Note

    You must have install permissions on the computer in order to run the installer.

    Note

    If the .NET Framework is not already installed on the target computer, this deployment will install it, and this installation might take several minutes.

To uninstall the application

  1. In Control Panel, double-click Add or Remove Programs.

  2. In the Add or Remove Programs dialog box, select Custom Action Installer and click Remove.

    Tip

    To uninstall from your development computer, with the Custom Action Installer project open and selected in Solution Explorer, from the Project menu, click Uninstall.

See Also

Other Resources

Custom Actions Management in Deployment