Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

260 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

In the preceding chapters, you created the Customer Maintenance project for CareKar, Inc. However, until now, you have not deployed the project at the client site. Visual Studio .NET provides you with the functionality to deploy the application that you have created on any other computer. You can also distribute your application on another computer in the form of a program that can be easily installed on the computer. In this chapter, you will learn to deploy a Windows

application.

Introduction to Deploying a Windows Application

In real-life situations, you often need to execute a Windows application that you have created on a computer other than the computer on which you created the application. This is called deploying a Windows application. Deploying a Windows application in Visual Studio .NET can be as simple as compiling the application in the form of an .exe file. You can then execute the application by copying the

.exe file of the application on another computer.

However, for huge applications, like the one that you have created for CareKar, Inc., compiling the application as an .exe file may not guarantee the successful deployment of the application. In such cases, you need to create an installation program to deploy your application on another computer. The user can then run the installation program that copies the installation files to the user’s computer. In addition, the user is not required to explicitly make changes to the registry of the computer. The installation program modifies the registry, enabling the application to run on the user’s computer.

To execute an application in Visual Studio .NET, the application is first converted to managed code that is managed by the CLR (common language runtime). To do so, the installation program makes the CLR files, which are required for the execution of the application, available to the application.

DEPLOYING A WINDOWS APPLICATION

Chapter 12

261

 

 

 

 

The process of deploying an application as an installable program on the user’s machine requires you to decide on the location where you need to deploy the application. In addition, you need to identify the method by which the application is to be deployed. To create an installation program for your application, you can use various deployment projects available in Visual Studio .NET, which are discussed in the following section.

Deployment Projects Available in Visual Studio .NET

A deployment project in Visual Studio .NET is a project that enables you to create an installation program to ensure a successful deployment of your application on another computer. Figure 12-1 displays various deployment projects provided by Visual Studio .NET.

FIGURE 12-1 Various deployment projects in Visual Studio .NET

You can choose the type of deployment project to be used depending on the type of application that you create.

262 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

The CAB Project

The simplest way to deploy a Windows application is to convert the application to a CAB (cabinet) file. A CAB file is a compressed form of your project. This implies that a CAB file compresses the application into smaller files that occupy less memory on the user’s computer. Therefore, converting an application to a CAB project enables the user to store the application in a compressed and organized manner. In addition, the CAB files that you create for your project can be easily transported and deployed on the user’s machine.

A CAB file can be used to package the ActiveX controls. Packaging an ActiveX control involves signing the ActiveX control or the application that contains the control. This process is called Authenticode signing. This enables the user to identify the source of the application and verify its authenticity. In addition, users can easily download and then install these files on their machines. You will learn about packaging Web applications that can be downloaded from a Web server in Chapter 26, “Deploying the Application.”

To enable you to convert your application into a CAB file, Visual Studio .NET provides the Cab Project template. To access the Cab Project template, perform the following steps:

1.On the File menu, point to the Add Project option.

2.From the list that is displayed, select the New Project option. The Add New Project dialog box is displayed.

3.In the Project Types: pane of the Add New Project dialog box, select the Setup and Deployment Projects option.

Various options of deployment projects available in Visual Studio .NET are displayed in the Templates: pane.

4.Select the Cab Project option.

Figure 12-2 shows the Cab Project option.

DEPLOYING A WINDOWS APPLICATION

Chapter 12

 

263

 

 

 

 

 

 

FIGURE 12-2 The Cab Project option in the Add New Project dialog box

5. In the Name: text box, type the name of the Cab Project as Customer-

MaintenanceCabProject.

6.Browse for the location where you want to save CustomerMaintenanceCabProject by using the Browse button.

7.Click on the OK button to close the Add New Project dialog box.

CustomerMaintenanceCabProject is added to the Solution Explorer window. Figure 12-3 shows CustomerMaintenanceCabProject added to the Solution Explorer window.

264 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

FIGURE 12-3 CustomerMaintenanceCabProject in the Solution Explorer window

The Properties window of the CustomerMaintenanceCabProject project displays information about the project,such as the name, version number, and Web dependencies of the project.

TIP

Visual Studio .NET does not specify any Web dependencies of the CAB projects. However, you can create references to any Web dependencies by changing the Web dependencies property of the CAB project. If Web dependencies property is set, all dependencies are automatically downloaded and installed when the CAB file is run.

You can also implement Authenticode signing by checking the Authenticode signing: check box in CustomerMaintenanceCabProject Property Pages. To access CustomerMaintenanceCabProject Property Pages, perform the following step:

1.Click on the View menu and select the Property Pages option. Alternatively, you can press the Shift+F4 keys.

CustomerMaintenanceCabProject Property Pages is displayed. Figure 12-4 shows CustomerMaintenanceCabProject Property Pages.

DEPLOYING A WINDOWS APPLICATION

Chapter 12

265

 

 

 

 

FIGURE 12-4 CustomerMaintenanceCabProject Property Pages

You can also specify the amount by which you want to compress the application in the properties of the CAB project. For example, if you compress an application by a higher amount, the file creation process takes more time compared to compressing a file by lower amounts. However, a file with higher compression level takes less time to download. In addition, the properties of a CAB project allow you to specify the location where you want to store the executable files.

However, the CustomerMaintenanceCabProject project that you have created does not contain the application files. To add the application files to the CAB project, perform the following steps:

1.Right-click on the CustomerMaintenanceCabProject project in the Solution Explorer window.

2.In the displayed list, point to the Add option. The displayed list contains the following options.

Project Output. The Project Output option displays the Add Project Output Group dialog box that provides several options of files that you can add to the CAB project. Table 12-1 displays the file options in the Add Project Output Group dialog box.

File. The File option enables you to add an arbitrary file other than the files listed in Table 12-1 of the CAB project.

266

Project 1

CREATING A CUSTOMER MAINTENANCE PROJECT

 

 

 

 

 

 

Table 12-1 File Options in the Add Project Output Group dialog box

 

 

 

 

 

 

File Options

Description

 

 

 

 

 

 

Documentation Files

Documentation Files contain the documentation of the project.

 

 

Primary Output

The Primary Output files contain the executable files built by the

 

 

 

 

project.

 

 

Localized Resources

The Localized Resources files contain the assembly information

 

 

 

 

about the resources used in the project.

 

 

Debug Symbols

The Debug Symbols files contain the debugging files required for

 

 

 

 

the project.

 

 

Content Files

Content Files contain all the content files used in the project.

 

 

Source Files

Source Files contain all the source files used in the project.

 

 

 

 

 

After learning about the types of files that Visual Studio .NET allows you to add to the CAB project, you can continue with the steps to add files to the CustomerMaintenanceCabProject project that you have created.

3.Select the Project Output option.

The Add Project Output Group dialog box is displayed. The name of the project is displayed in the Project: list box.

4.Select the Primary Output option from the file options in the Add Project Output Group dialog box.

TIP

The Primary Output option is a mandatory option. However, you may choose to select any other option to be deployed along with the application.

5.Click on the OK button to close the Add Project Output Group dialog box.

The Primary Output option is displayed in the Solution Explorer window. You can view the file name and the path of the executable file in the Properties window of the Primary Output option.

DEPLOYING A WINDOWS APPLICATION

Chapter 12

 

267

 

 

 

 

 

 

6.Select the Primary Output option in the Solution Explorer window to display the Properties window.

7.Click the ellipsis button of the Outputs property.

8.The Outputs dialog box is displayed, which contains information about the executable file for the Customer Maintenance project.

Figure 12-5 displays the Outputs dialog box.

9.Build the project by clicking on the F5 key.

Alternatively, you can select the Start option on the Debug menu.

FIGURE 12-5 The Outputs dialog box

To create the executable file, you need to build the CustomerMaintenanceCabProject project, as described in the previous Step 9.

Building the project creates an executable file in the location specified in the Outputs dialog box. The executable file, along with an .osd file, is created in the form of a compressed file. To access these files, you can unzip the CustomerMaintenanceCabProject.CAB file. The .osd file contains the information about the CustomerMaintenanceCabProject.CAB file in the XML format. Figure 12-6 shows the contents of the .osd file.

268 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

FIGURE 12-6 Contents of the .osd file

 

T

 

 

For huge projects, a CAB project may not be sufficient to deploy an application. Therefore, you can combine the CAB project option with the other options provided by Visual Studio .NET. For example, consider the Customer Maintenance project that we have created for CareKar, Inc. This project includes several resource files, such as .xsd files, that contain information about the datasets created to access the tables in the CMS (Customer Maintenance System) database. Because these .xsd files are included in the application, they need to be distributed as a part of the application.In such a scenario, it would be appropriate to first convert the application into a CAB file and then create a Setup project. I will discuss the Setup project in the following section.

The Setup Project

Another deployment project that Visual Studio .NET provides you is the Setup project.The Setup Project template creates the installer files that users can install on their machines to deploy the application. The installer files created by the Setup Project template are called MSI (Microsoft Windows Installer) files. These files have an extension of .msi and can be installed on the user’s machine by using the Microsoft Installer service.

Team-Fly®

DEPLOYING A WINDOWS APPLICATION

Chapter 12

269

 

 

 

 

The Setup Project template creates the MSI files for your application, which include the application files, the resource files, and the information required for the deployment of the application. This information includes the registry information and the steps for the successful installation of the application. In addition, the MSI files include the Visual Studio. NET runtime files that are required for the execution of the Windows application.

THE MICROSOFT INSTALLER SERVICE

The Microsoft Installer service is an installation service provided by Microsoft to optimize the process of deploying an application. For example, the Microsoft Installer service installs the files required for the successful deployment of an application or the component of an application. This service is available as a part of Microsoft Windows 2000 and higher operating systems.

You will now create a Setup project for

the Customer Maintenance project by using the Setup templates provided by Visual Studio .NET. Visual Studio .NET provides separate templates for deploying the Windows application and Web applications.The template used to deploy the Windows application,the Setup Project template, creates the MSI files for the application on the user’s computer. The template used to deploy Web applications, the Web Setup Project template, creates the MSI files in a virtual directory present on a Web server. In this chapter, I will be discussing the deployment of the Windows application by using the Setup Project template.

To create a Setup project for the Customer Maintenance project, perform the following steps:

1.On the File menu, point to the Add Project option.

2.From the list that is displayed, select the New Project option. The Add New Project dialog box is displayed.

3.From the Project Types: pane, select the Setup and Deployment Projects option.

4.In the Templates: pane, select the Setup Project option.

5.In the Name: text box, type the name of the Setup project as CustomerMaintenanceSetupProject.

6.Click on the Browse button to browse to the location where you want to save the Setup project.

7.Click on the OK button to close the Add New Project dialog box.

270 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

The Setup Project template creates a file system editor, which is displayed by default. You can also access the file system editor from the View menu. In addition to the file system editor, the View menu provides several other editor options, such as Registry, File Types, User Interface, Custom Actions, and Launch Conditions. To access the editors provided by Visual Studio .NET, perform the following steps:

1.Right-click on CustomerMaintenanceSetupProject in the Solution Explorer window.

2.In the displayed list, point to the View menu.

The list of file editors is displayed. You can click on any of these options to display the corresponding information. You will learn more about the editors later in this chapter.

Figure 12-7 displays the file system editor as created on the user’s machine.

FIGURE 12-7 File system editor as created on the user’s machine

As you can see, the folders in the file system editor, such as Application Folder, User’s Desktop, and User ’s Program Menu, are empty. This is because you have not added the output files to the Setup project. You will learn to add the output files later in this chapter.

DEPLOYING A WINDOWS APPLICATION

Chapter 12

271

 

 

 

 

The Add option of CustomerMaintenanceSetupProject provides you with two options in addition to the Project Output and File options. These additional options are Folder and Assembly.

Folder. The Folder option allows you to add a new folder to the file system editor.

Assembly. The Assembly option allows you to add Visual Studio .NET components from the Component Selector dialog box. The Component Selector dialog box contains a list of components, their versions, and their locations on the hard disk, which you may need to add to the user’s machine. Figure 12-8 displays the Component Selector dialog box.

FIGURE 12-8 The Component Selector dialog box

After seeing the options available on the Add menu, you can continue with the process of creating a Setup project. In this project, you will be adding only Project Output. However, you may add the other options to your project, if required.

1.In the user interface for the file system editor, right-click Application Folder.

2.In the displayed list, point to the Add menu and then select the Folder option.

272 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

Visual Studio .NET adds a new folder to the file system editor. Alternatively, you can add a new folder by clicking on the Action menu. In the displayed list, point to the Add menu and then select the Folder option.

3.Name this folder Output.

You may give any name to the folder.

4.On the Action menu, point to the Add option.

5.Select the Project Output option to add the required files to the Setup project.

The Add Project Output Group dialog box is displayed.

6.In the Add Project Output Group dialog box, select the Primary Output option.

The Primary Output is created in the Output folder.

You saw the user interface for the file system earlier. The folders did not contain the output files.After adding the Project Output file to the Setup project, the user interface for the file system appears as shown in Figure 12-9.

FIGURE 12-9 The user interface for the file system editor

DEPLOYING A WINDOWS APPLICATION

Chapter 12

273

 

 

 

 

After creating the output file, you need to build the Setup project by performing the following steps:

1.Click on the Build menu.

2.From the drop-down list, select the Build CustomerMaintenanceSetupProject option.

Building the project creates a MSI file that can be easily deployed on the user’s machine. The location and other properties of the MSI file are displayed in the Properties window of the Primary Output file. Figure 12-10 shows the CustomerMaintenanceProject.msi file.

FIGURE 12-10 The CustomerMaintenanceProject.msi file

You can distribute the CustomerMaintenanceProject.msi file that you have created in several ways, such as floppy disks or compact discs. To do this, copy the MSI file that is created to the distribution medium and then run the installation program on the user’s machine.

Perform the following steps to install the Windows application on the user’s machine.

1. Copy the CustomerMaintenanceProject.msi file to the user’s machine.

274Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

2.Double-click on the CustomerMaintenanceProject.msi file to start the installation.

The Windows Installer service prepares the user’s machine for the installation. Then, the Welcome page of the Setup wizard is displayed.

3.Click on the Next button to continue.

The Select Installation Folder page is displayed.

4.Browse for the location where you want to install the application by clicking on the Browse button.

5.Select the Everyone radio button if you want to enable all the users who log on to the machine to access the application.

By default, the Just me radio button is selected.

6.Click on the Next button to continue.

The Confirm Installation page is displayed.

7.Click on the Next button to start the installation.

A progress bar shows the progress of the installation process. When the installation process is complete, the Installation Complete page is displayed.

8.Click on the Close button to complete the installation.

The CustomerMaintenanceProject.exe file is created in the specified folder.

9.Double-click the CustomerMaintenanceProject.exe file to run the Windows application.

Having tested the application, you can distribute the application to your customer.

Merge Module

In addition to a CAB or Setup project, you can create a Merge Module project by using the templates provided by Visual Studio .NET. A Merge Module project is used to combine the application files, resource files, registry files, and Setup files in a single package.

You use the Merge Modules for projects that can be shared across applications. This implies that the components used to set up a Merge Modules project can be

DEPLOYING A WINDOWS APPLICATION

Chapter 12

275

 

 

 

 

shared for multiple Merge Module projects. For example, consider a situation in which you need to distribute two applications on a user’s computer. In this case, you can have a common set of setup components for both the applications.Therefore, the Merge Module projects are similar to the dynamic link library (.dll) files, which allow applications to share the code.

In addition to the setup components, you can create any component as a Merge Module that needs to be shared across multiple applications. For example, if a resource is used in more than one application, you can deploy the resource as a Merge Module and can then reuse the resource file for multiple applications. However, you cannot install a Merge Module alone. It can be added to the MSI files that you have created in the previous section.

TIP

You can add a Merge Module component while creating a MSI file. In addition, the merge module component can be added after the MSI files are created.

Another advantage of creating a Merge Module project is that the project recognizes all the dependencies for a component and tracks the versions of the component. This prevents the user from installing the incorrect version of the component. To further avoid any version problem while installing a component, you must create a Merge Module project that contains the dependencies of the component.

You will now learn how to create a Merge Module project. You can create a Merge Module project similar to the way you created the Setup Project.However, instead of selecting the Setup Project option in the Add New Project dialog box, select the Merge Module option. After performing the required steps, build the Merge Module project to create a .msm file. Figure 12-11 shows the file system editor of the Merge Module project.

276 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

FIGURE 12-11 The file system editor of the Merge Module project

As discussed earlier, to deploy the .msm file, you need to merge the file with a Windows Installer (.msi) file. The MSI file that contains a Merge Module component also stores information about the version of the component.

While you install the application, the Windows Installer service adds the version information to a Windows Installer database that enables multiple applications to use a component. If you uninstall any one application, the Windows Installer database ensures that the corresponding component is not uninstalled.

In the preceding sections, you looked at creating various deployment projects by using the templates provided by Visual Studio .NET. However, you can create these deployment projects by using the Setup wizard.

The Setup Wizard

The Setup wizard is used to create various deployment projects. You can now create a deployment project by using the Setup wizard. To access the Setup wizard, select the Setup Wizard option in the Add New Project dialog box.The Welcome

DEPLOYING A WINDOWS APPLICATION

Chapter 12

277

 

 

 

 

page of the Setup wizard is displayed. To create the deployment project by using the wizard, perform the following steps.

1.On the Welcome page of the wizard, click on the Next button to start the Setup wizard.

The Choose a project type page is displayed.

2.Click on the radio button to create the type of deployment project.

The wizard provides you with an option to create a Setup project for a Windows application, a Setup project for a Web application, a Merge Module for a Windows Installer, and a downloadable CAB file.

3.Click on the Next button to display the Choose project outputs to include page.

4.In the Which project output groups do you want to include? text box, check the file options that you want to include in your deployment project, and click on the Next button.

The Choose files to include page is displayed. This page allows you to add any additional file other than the files that you added in Step 4, such as .txt or .htm files. You can add a file by clicking on the Add button. Because adding additional files is optional, you may choose to proceed further without adding any additional files.

5.Click on the Next button to display the Create Project page.

The Create Project page displays the summary of the information that you have specified in the Setup wizard. Figure 12-12 displays the summary of the information specified in the Setup wizard.

6.To create the project, click on the Finish button on the Create Project page.

278 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

 

 

Y

 

L

 

F

 

M

 

A

 

FIGURE 12-12 Summary of the information specified in the Setup wizard

E

 

T

 

 

The project created by the wizard is added to the Solution Explorer window. You can now build the deployment project to test it.

When you build the project, Visual Studio .NET creates the output file in the Output folder that you created in the Application Folder. However, Visual Studio

.NET enables you to create a shortcut to the output file on the user’s machine. The user can then conveniently access the output file by using this shortcut. You can then create a shortcut to the output file.

It is a good practice to create a shortcut for users so that they can easily access your application. To create a shortcut, perform the following steps:

1.Select the Primary output from CustomerMaintenanceProject (Active) file in the Output folder.

2.On the Action menu, select the Create Shortcut to Primary output from CustomerMaintenanceProject (Active) option.

A shortcut is created in the Output folder. If required, you can rename the shortcut.

Team-Fly®