How to create an Installer for a Winforms application using Wix for Visual Studio 2022
Tags: dotnet, csharp, programming, learning
Published: 2024-05-02 12:10:21

In this tutorial, we’ll look at creating an installer for a Winforms desktop application using Wix that we can share with people.

**Prerequisites:**
The only prerequisite for this tutorial is Visual Studio Community edition, go ahead and download the installer [here](https://visualstudio.microsoft.com/downloads/)

After downloading, go ahead and install and open Visual Studio.

Next, we want to create a project, we will create a small Winforms application for this demo.

I have created a Winforms application in a previous post [here](https://dev.to/bigboybamo/how-to-create-an-installer-for-a-winforms-application-using-visual-studio-2022-installer-project-5nh), you can follow that link, create your application and then come back here.

When you're done configuring the project, go ahead and install the Wixv3 - Visual Studio 2022 extension we’ll use to setup our installer. In the Visual Studio top tab click **Extensions** and then click **Manage Extensions**
Search for installer projects, You will see Wix, click download


![Install Wix](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8kg36kqay12fsr2wb6c.png)

After downloading, Close your Visual Studio instance.  
The VSIX installer comes up, go ahead and click modify.

After the modification is done and the extension is installed, go ahead and reopen your project again in Visual Studio. The extension should be added to your installed extensions.

We also need to install WiX Toolset v3.11.2, you can download the latest version from [here](https://github.com/wixtoolset/wix3/releases/tag/wix3112rtm)

After downloading, simply run the installer and follow the prompts to install.

You might also be prompted to install .NET Framework 3.5, please follow the official Microsoft documentation on how to set that up [here](https://learn.microsoft.com/en-us/dotnet/framework/install/dotnet-35-windows#download-the-offline-installer)

Next, right-click on your project solution and add a new project.

In the Add project modal, we will see a list of new projects that have been installed. Search **Wix**


![Add Wix project to solution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ex0korylnmourdpaldxj.png)

Give your project a name, I have called mine **WixSetup**, and click Create.

After successfully creating it, your solution should look like this.

![Wix project file structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c04638a23pnlyjf7nfa4.png)

Great, a new Xml file called `Product.wxs` was generated, this will hold all our logic. Let's go ahead and make some changes

First, change  the `Manufacturer` in the Product element, I will change this to my name
```cs
<Product Id="*" Name="WixSetup" Language="1033" Version="1.0.0.0" Manufacturer="Olabamiji" UpgradeCode="71fc5c9b-11ff-4b7c-b97a-320513ae22d4">
```

Next in the 2nd `Fragement` element, let us configure what we want to add to our installer; so change your code so it looks like this

```cs
	<Fragment>
		<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
			<Component Id = "WixApp" Guid= "71fc5c9b-11ff-4b7c-b97a-320513ae22d4">
				<File Id = "WixApp" Name= "SetupInstallerTutorial.exe" Source= "..\SetupInstallerTutorial\bin\Debug\net8.0-windows\">
					</File>
		</Component>
		</ComponentGroup>
	</Fragment>
```

Alright, We have made some changes, let's go through what we have added;
We added a `Component` and Specified an Id/Name for it. Then in that component we added a `Guid`, which acts as a unique identifier for that component, you can generate any guid you like. 

Then inside the component element, we added a child node element called `File`, and gave it an `Id` that corresponds with our `Component `Id. We also gave the file a name, which is the name of the .exe file that is generated when you build your winforms application. Finally, the source is simply where that winforms application .exe is created (bin folder of your project's directory)

Now in your solution explorer, right-click on the Wixsetup project and click build

![Build Wix project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9f7yrlvziwqsnft48iu9.png)

After building, Navigate to the bin folder of your **Wix** installer project.

![Installer file location](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgd7u2o9vj349c8hsvfj.png)

We see that the installer has been generated and **cab1** and **Wixsetup.wixpdb** files hold information about the files we add in our  `Product.wxs` file.

And that’s it. Our installer for our application has been built, you can go ahead and run the `WixSetup` file to install your WinForms application.

Happy coding everyone.