Create setup project using .net

来源:互联网 发布:软件项目成本预算表 编辑:程序博客网 时间:2024/05/17 08:49

Chapter 1:Walkthrough: Deploying a Windows Application

This walkthrough demonstrates the process of creating an installer for a Windows application that launches Notepad. In this walkthrough, you will create the Windows application, then create an installer that sets up shortcuts and file associations, adds an entry to the registry, displays custom dialogs, and checks the version of Internet Explorer during installation.

Note   When creating your own installers, not all of these steps are necessary. This walkthrough is intended to introduce you to some of the optional capabilities of deployment that you may find useful. To create a basic installer, complete the first four sets of procedures.

To create a Windows application

  1. On the File menu, point to New, and choose Project.
  2. In the New Project dialog box, select Visual Basic Projects in the Project Types pane, and then choose Windows Application in the Templates pane. In the Name box, type My Notepad.
  3. Click OK to close the dialog box.

    The project is added to Solution Explorer, and the Windows Forms Designer opens.

  4. Select the Windows Forms tab in the Toolbox and drag a Button control onto the form.
  5. Double-click the Button control to add an event handler for the button. In the event handler, add the following code:
    Copy Code
    Shell("Notepad.exe", AppWinStyle.NormalFocus)

    This will launch Notepad.exe and give it focus.

  6. On the Build menu, choose Build My Notepad to build the application.

To create a deployment project

  1. On the File menu, point to Add Project, and choose New Project.
  2. In the Add New Project dialog box, select Setup and Deployment Projects in the Project Types pane, and then choose Setup Project in the Templates pane. In the Name box, type My Notepad Installer.
  3. Click OK to close the dialog box.

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

  4. Select the My Notepad Installer project in Solution Explorer. In the Properties window, select the ProductName property and type My Notepad.
    Note   The ProductName property determines the name that will be displayed for the application in folder names and in the Add/Remove Programs dialog box.

To add the Windows application to the installer

  1. Select the My Notepad Installer project in Solution Explorer. In the File System Editor, select the Application Folder node.
  2. On the Action menu, choose Add, Project Output.
  3. In the Add Project Output Group dialog box, choose My Notepad from the Project drop-down list. Click OK to close the dialog box.
  4. Select the Primary Output group from the list, then click OK.
  5. On the Build menu, choose Build My Notepad Installer.

To deploy the application (basic installer)

This step can be skipped if you are following the complete walkthrough.

  • Select the My Notepad Installer project in Solution Explorer. On the Project menu, choose Install.

    This will run the installer and install My Notepad on your development computer.

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

Optional Deployment Capabilities

The remaining steps demonstrate optional deployment capabilities.

Creating a Shortcut

This step creates a shortcut to your application that will be placed on the desktop of a target computer during installation.

To create shortcuts for the Windows application

  1. Select the My Notepad Installer project in Solution Explorer.
  2. In the File System Editor, select the Primary output from My Notepad node.
  3. On the Action menu, choose Create Shortcut to Primary Output from My Notepad.

    This will add a Shortcut to Primary output from My Notepad node.

  4. Rename the shortcut Shortcut to My NOTEPAD.
  5. Select Shortcut to My NOTEPAD and drag it to the User's Desktop folder in the left pane.

Creating a File Association

This step adds a file association for My Notepad so that double-clicking .vbn files will launch the My Notepad application.

To create file associations for the Windows application

  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, point to Editor, and choose File Types.
  2. Select the File Types on Target Machine node in the File Types Editor. On the Action menu, choose Add File Type.

    A New Document Type #1 node is added and opened for renaming.

  3. Rename New Document Type #1 as Vbn.doc.
  4. In the Properties window, set the Extension property of the file type to vbn.
  5. Select the Command property and click the Ellipsis () button. In the Select item in project dialog box, navigate to the Application Folder, and select Primary output from My Notepad.
  6. Click OK to close the dialog box.

Adding a Registry Entry

This step adds a registry key and value to the registry. You could reference this registry key from your application's code to retrieve per-user information at run time.

To add a registry entry for the Windows application

  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, point to Editor, and choose Registry.
  2. Select the HKEY_CURRENT_USER node and expand it, then expand the Software node and select the [Manufacturer] node.
    Note   The Manufacturer node is surrounded by brackets to denote that it is a property. It will be replaced by the value entered for the Manufacturer property for the deployment project.
  3. On the Action menu, choose New, Key.
  4. Rename the key UserChoice.
  5. Select the UserChoice key.
  6. On the Action menu, point to New, then click String Value.
  7. Rename the value TextColor.
  8. In the Properties window, select the Value property and enter Black.

Adding a Custom Installation Dialog Box

This step adds and configures a custom user interface dialog box that will be displayed during installation.

To add a custom installation dialog box

  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, point to Editor, and choose User Interface.
  2. In the User Interface Editor, select the Start node under the Install node.
  3. On the Action menu, choose Add Dialog.
  4. In the Add Dialog dialog box, choose Checkboxes (A).
  5. Click OK to close the dialog box.
  6. On the Action menu, choose Move Up twice to position the Checkboxes (A) dialog box above the Installation Folder dialog box.
  7. In the Properties window, set the BannerText property to Samples.
  8. Set the BodyText property to The Install Samples check box controls whether or not the sample files are installed. If left unselected, the samples will not be installed.
  9. Set the CheckBox1Label property to Install samples?
  10. Set the properties Checkbox2Visible, Checkbox3Visible, and Checkbox4Visible to false. This will hide the additional check boxes.

Working with Samples

This step creates a Samples subfolder that will be installed beneath the Application folder.

To add a Samples folder

  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, point to Editor, and choose File System. The Application Folder should still be selected.
  2. From the Action menu, point to Add, and choose Folder.
  3. Rename "New Folder #1" as Samples.

This step creates two sample text files that will be installed if the user chooses the Install samples option in the custom dialog.

To create sample files for the application

  1. Using Notepad or another text editor, create a text file containing the text This is rules.vbn. Save it as Rules.vbn.
    Note   To prevent Notepad from automatically adding a .txt extension, choose All Files in the Files of type drop-down list.
  2. Create another text file containing the text This is memo.vbn. Save it as Memo.vbn.

This step adds the sample files to the Samples folder, and sets a condition that determines whether to install the files.

To add the samples to the installer

  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, point to Editor, choose File System, and select the Samples folder.
  2. From the Action menu, point to Add, and choose File. Add the Rules.vbn and Memo.vbn files to the Samples folder.
  3. Select the file Rules.vbn in the File System Editor.
  4. In the Properties window, set the Condition property to CHECKBOXA1=1. When the installer is run, the file Rules.vbn will only be installed if the custom check box is selected.
  5. Select the file Memo.vbn in the File System Editor.
  6. In the Properties window, set the Condition property to CHECKBOXA1=1. When the installer is run, the file Memo.vbn will only be installed if the custom check box is selected.

Adding Launch Conditions

This step checks to see if Internet Explorer 5.0 or higher is installed on a target computer, and halts installation if it is not installed.

To add a launch condition to check the Internet Explorer version

Note   This step is intended to demonstrate the concept of launch conditions; the My Notepad application has no actual dependency on Internet Explorer.
  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, point to Editor, and choose Launch Conditions.
  2. In the Launch Conditions Editor, select the Requirements on Target Machine node.
  3. On the Action menu, choose Add File Launch Condition.

    A Search for File1 node is added beneath the Search Target Machine node, and a Condition1 node is added beneath the Launch Conditions node.

  4. Rename Search for File1 to Search for Internet Explorer.
  5. In the Properties window, set the FileName property to Iexplore.exe, the Folder property to [ProgramFilesFolder], the Depth property to 2, and the MinVersion property to 5.00.
  6. Select the Condition1 node.
  7. Set the Message property to This program requires Microsoft Internet Explorer 5.0 or higher. Please install Internet Explorer and rerun the Notepad installer.

Setting Optional Properties

This step sets a property to automatically install the Windows Installer bootstrapping application files if the correct version of Windows Installer is not present on the target computer.

To set optional properties for the deployment project

  1. Select the My Notepad Installer project in Solution Explorer. On the View menu, choose Property Pages.
  2. On the My Notepad Installer property page, select the Bootstrapper drop-down list, and choose Windows Installer Bootstrapper.
  3. On the Build menu, choose Build My Notepad Installer.

Installing on Your Development Computer

This step will run the installer and install My Notepad on your development computer.

To install My Notepad on your development computer

  • Select the My Notepad Installer project in Solution Explorer. On the Project menu, choose Install.
    Note   You must have install permissions on the computer in order to run the installer.

Deploying to Another Computer

This step will run the installer and install My Notepad on another computer.

To deploy My Notepad to another computer

  1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be /documents and settings/yourloginname/My Notepad Installer/project configuration/My Notepad Installer.msi. The default project configuration is Debug.
  2. Copy the My Notepad Installer.msi file 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 the Setup.exe file to run the installer.
    Note   You must have install permissions on the computer in order to run the installer.

Testing

This step will test starting the installer from a desktop shortcut and uninstalling.

To test the installation

  1. Verify that the shortcut is installed on the desktop and that it correctly launches the My Notepad application.
  2. Uninstall the application using the Add/Remove Programs tool in Control Panel.
    Tip   To uninstall from your development computer, on the Project menu, choose Uninstall.

 chapter 2: Walkthrough: Using a Custom Action to Create a Database During Installation

The following walkthrough demonstrates the use of a custom action and the CustomActionData property to create a database and database table during installation.

Note   This walkthrough requires SQL Server on the computer where you will deploy the application.

To create an installer class

  1. On the File menu, point to New, and then choose Project.
  2. In the New Project dialog box, select Visual Basic Projects in the Project Type pane, and then choose Class Library in the Templates pane. In the Name box, type DBCustomAction.
  3. Click OK to close the dialog box.
  4. On the Project menu, choose Add New Item.
  5. In the Add New Item dialog box, choose Installer Class. In the Name box, type DBCustomAction.
  6. Click OK to close the dialog box.

To create a data connection object

  1. In Server Explorer, select Data Connections. Right-click and choose Add Connection.
  2. In the Data Link Properties dialog box, do the following:
    1. Enter the server name.
    2. Select Use Windows NT Integrated Security.
    3. In the database box, type master.
    4. Click OK to close the dialog box.
  3. Drag the new connection and drop it on the DBCustomAction.vb designer to create a sqlConnection1 object.

To create a text file that contains a SQL statement to create a database

  1. In Solution Explorer, select the DBCustomAction project. On the Project menu, choose Add New Item.
  2. In the Add New Item dialog box, choose Text File. In the Name box, type sql.txt (must be in lower case).
  3. Click OK to close the dialog box.
  4. Add the following to the sql.txt file:
    Copy Code
    CREATE TABLE [dbo].[Employees] ([Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[Rsvp] [int] NULL ,[Requests] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY];ALTER TABLE [dbo].[Employees] WITH NOCHECK ADD CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED ([Name]) ON [PRIMARY];
  5. In Solution Explorer, select sql.txt. In the Properties window, set the BuildAction property to Embedded Resource.

To add code to the installer class to read the text file

  1. In Solution Explorer, select DBCustomAction.vb. On the View menu, choose Code.
  2. Add the following Imports statement at the top of the module:
    Copy Code
    Imports System.IOImports System.Reflection
  3. Add the following code to the class:
    Copy Code
    Private Function GetSql(ByVal Name As String) As String   Try      ' Gets the current assembly.      Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()      ' Resources are named using a fully qualified name.      Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name)      ' Reads the contents of the embedded file.      Dim reader As StreamReader = New StreamReader(strm)      Return reader.ReadToEnd()   Catch ex As Exception      MsgBox("In GetSQL: " & ex.Message)      Throw ex   End TryEnd FunctionPrivate Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As String)   Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)   Command.Connection.Open()   Command.Connection.ChangeDatabase(DatabaseName)   Try      Command.ExecuteNonQuery()   Finally      ' Finally, blocks are a great way to ensure that the connection       ' is always closed.      Command.Connection.Close()   End TryEnd SubProtected Sub AddDBTable(ByVal strDBName As String)   Try      ' Creates the database.      ExecuteSql("master", "CREATE DATABASE " + strDBName)      ' Creates the tables.      ExecuteSql(strDBName, GetSql("sql.txt"))   Catch ex As Exception       ' Reports any errors and abort.       MsgBox("In exception handler: " & ex.Message)       Throw ex   End TryEnd SubPublic Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)   MyBase.Install(stateSaver)   AddDBTable(Me.Context.Parameters.Item("dbname"))End Sub
  4. On the Build menu, choose Build DBCustomAction.

To create a deployment project

  1. On the File menu, choose Add Project, New Project.
  2. In the Add Project dialog box, select Setup and Deployment Projects in the Project Type pane, and then choose Setup Project in the Templates pane. In the Name box, type DBCustomAction Installer.
  3. Click OK to close the dialog box.
  4. In the Properties window, select the ProductName property and type DB Installer.
  5. In the File System Editor, select the Application Folder. On the Action menu, choose Add, Project Output.
  6. In the Add Project Output Group dialog box, select the primary output for the DBCustomAction project. Click OK to close the dialog box.

To create a custom installation dialog

  1. Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose User Interface.
  2. In the User Interface Editor, select the Start node under Install. On the Action menu, choose Add Dialog.
  3. In the Add Dialog dialog box, select the Textboxes (A) dialog, then click OK to close the dialog box.
  4. On the Action menu, choose Move Up. Repeat until the Textboxes (A) dialog is above the Installation Folder node.
  5. In the Properties window, select the BannerText property and type Specify Database Name.
  6. Select the BodyText property and type This dialog allows you to specify the name of the database to be created on the database server.
  7. Select the Edit1Label property and type Name of DB:.
  8. Select the Edit1Property property and type CUSTOMTEXTA1.
  9. Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to false.

To create a custom action

  1. Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose Custom Actions.
  2. In the Custom Actions Editor, select the Install node. On the Action menu, choose Add Custom Action.
  3. In the Select item in project dialog box, double-click the Application Folder.
  4. Select the Primary output from DBCustomAction(Active) item, then click OK to close the dialog box.
  5. In the Properties window, select the CustomActionData property and type /dbname=[CUSTOMTEXTA1].
  6. On the Build menu, choose Build DBCustomActionInstaller.

To install on your development computer

  • Select the DBCustomAction Installer project in Solution Explorer. On the Project menu, choose Install.

    This will run the installer on your development computer.

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

To deploy to another computer

  1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be /documents and settings/yourloginname/DBCustomAction Installer/project configuration/DBCustomAction Installer.msi. The default project configuration is Debug.
  2. Copy the DBCustomAction Installer.msi file 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.

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

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

To uninstall the application

  1. In the Windows Control Panel, double-click Add/Remove Programs.
  2. In the Add/Remove Programs dialog box, select DBCustomAction Installer and click Remove, then click OK to close the dialog box.
    Tip   To uninstall from your development computer, on the Project menu, choose Uninstall.

 

 

 

 

Tips: if you want to pass more than two parameters to the installer class, you can indicate Custom Action--CustomActionData like "/dbname=[CUSTOMTEXTA1] /dbnamenew=[CUSTOMTEXTA2]".

Reference:http://msdn2.microsoft.com/en-us/library/49b92ztk(VS.71).aspx

http://msdn2.microsoft.com/en-us/library/k3bb4tfd(VS.71).aspx

http://www.yxyr.com/post/106.html

http://www.c-sharpcorner.com/UploadFile/vishnuprasad2005/SetupProjects12022005022406AM/SetupProjects.aspx

The following code is the source code in the installer class, it is some main method

 

private string GetSql(string name)
         {
              try
              {
                   Assembly asm = Assembly.GetExecutingAssembly();
                   Stream stream = asm.GetManifestResourceStream(asm.GetName().Name +"." + name);
                   StreamReader reader = new StreamReader(stream);
 
                   return reader.ReadToEnd();
              }
              catch (Exception ex)
              {
                   Console.WriteLine(ex.Message);
                   throw ex;
              }
 
 
         }
 
         private void ExecuteSql(string databaseName, string sql)
         {
              SqlCommand command = new SqlCommand(sql, sqlConnection1);
              command.Connection.Open();
              command.Connection.ChangeDatabase(databaseName);
              try
              {
                   command.ExecuteNonQuery();
              }
              finally
              {
                   command.Connection.Close();
              }
 
         }
 
         private void AddDBTable(string dbName)
         {
              try
              {
                   // Creates the database.
                   ExecuteSql("master", "CREATE DATABASE " + dbName);
                   ExecuteSql(dbName, GetSql("sql.txt"));
              }
              catch(Exception ex)
              {
                   Console.WriteLine(ex.Message);
                   AddDBTable(this.Context.Parameters["dbnamenew"]);
              }
 
         }
 
         public override void Install(IDictionary stateSaver)
         {
              base.Install (stateSaver);
              AddDBTable(this.Context.Parameters["dbname"]);
             
             
         }

 

 

原创粉丝点击