Sunday, July 24, 2016

Visual Studio Installer Projects: Installing PDFs and other Content with an Install Project

As a developer I get dragged into the dreaded installer on occasion -- I'd rather be up to my elbows in Azure Web Services and Azure SQL. I was faced with an interesting problem for a client. Their WPF desktop application needed to display PDFs. To facilitate this the PDFs had to be added to the applications installer.

The solution associated with this project was ASNACalculatorMaster. This solution contained a WFP application, ASNACalculator, and an installer project, ASNACalculatorSetup:



The first step to include PDFs files in the WPF project was to create folder in which store. This is not required but it is simply part of good project organization. To do this you:

1) Right click on the project (such ASNACalculator above) and select Add | New Folder from the context menu (see below):


2) Once new folder is clicked a new editable folder is added to the project:


3) Change the name of the NewFolder to the desired folder name in which to store the content files (PDFs for this excample):



The content folder for this example is DataSheets.

Adding files is like adding file to any other folder. The files selected below are the files to to copy to the ASNACalculator project's DataSheets folder:


The infamous CTRL-C was use to selected the files above. After this navigate to Visual Studio's Solution Explorer and right click on the the DataSheets folder inside of ASNACalculator project:


Note in the previous screenshot that the Paste option is enabled. Selecting Paste will paste the files into the DataSheets folder:



At this pointed Visual Studio does not know how interpret the PDF files. They are not C# files so there is no way to build the files. To remedy this, select all of the PDF files in the DataSheets folder:


Right click on the folders thus displaying the context menu associated with the PDFs files in the project:


From the context menu select the Properties menu items. This displays the properties of all the selected files:


Under properties set Build Action to Content and set Copy to Output Directory to Copy if newer. An example of this is shown below:


The ASNACalculator project now understands that the PDFs are considered to be content of the project and should be copied into the build folder during a build. The PDF files in the build directory will be updated if newer files are placed DataSheets folder.

It is important to recognize that Visual Studio does not copy the PDFs file directly into the output directory. Instead Visual Studio copies the DataSheets folder including all the PDF files into the output directory (as shown below where the DataSheets folder is contained the Release folder of the build)::


At this point the WPF project, ASNACalculator, interprets the PDFs file as content. Now the setup project, ASNACalculatorSetup, needs to be changed to include content as part of the installer. This is achieved by right clicking on the installer project within Solution Explorer:


From the context menu show above select Project Output:


Selecting the Project Output menu items displays the Add Project Output Group show above. From this dialog select Content Files (shown above) and click on OK. Once OK is clicked from this dialog, the ASNACalculatorSetup appears as follows within Solution Explorer:


Notice above that the ASNACalculatorSetup project now contains an entry, Content Files from ASNA Calculator (Active). This means the PDFs (the conent) are included in the installer.

When installed the PDF files will be placed in DataSheets folder below the folder in which the ASNCalculator.exe is installed. An example of this is as follows:





No comments :

Post a Comment