C# Setup project

To distribute a a project smoothly there is a project type in Visual Studio called Setup with which you can create an installation package with ease. I’m going to show you how it’s done.
Begin with having a project that you want someone to install (this is somewhat vital). Select File => Add => New Project and select, under “Other Project Types/Setup and Deployment“, Setup Projekt and give it a nifty name:

Setup skärmen
Chose the right type of project

A setup project behaves a little bit different from your standard project. Directly after creating the project you’ll see the “File System“-view. If you happen to screw up and close it, you’ll reach reach it again by right clicking on the setup roejct and select View => File System

Here, you can select which files to place where during the installation. All files that should be placed in the installation folder must be added to the Application Folder. If we want to add our program as output we need to select Add => Project Output:

Chose application folder and right click in the right windows

Chose Primary Output

After this you’ve added what you need for a basic installation. Before release though, you need to set the project properties. The following picture shows which properties are important:

Change the marked properties

If you want to add a short cut on the desktop select User’s Desktop in the File System tab and right click. Select ‘Create New Short Cut’ and select the output project. The same thing goes for the program menu (but in User’s Programs Menu).
But, the application we use in this example utilizes a text file. It reads the file and outputs it, like this:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
 
        using (FileStream stream = new FileStream("SomeTextFile.txt", FileMode.Open))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
        }
 
        Console.Read();
    }
}

As you can see, we’re reading from a file called ‘SomeTextFile.txt’ and outputs the content. My project looks like this:

Project overview

The text file ‘SomeTextFile.txt’ is stored as Content Properties => BuildAction which is a build action you use sometimes. It means that the file, as opposed to Embedded, is not embedded but must be manually added to the project output.
Therefore, we must add the Content Files output in our Application Folder. Right click in the folder and select Add => Project Output and select Content Files instead of Primary Output.

Now we have all we need to run an installation, and we’ll do it through Visual Studio to test it. Start by right clicking on the setup project and build it. The setup projects aren’t automatically added to the Visual Studio building queue and they generally require individual building actions (you can add them yourself, but since you very seldom need to build them there is no need for us to do it).
After you’ve built it, right click on it and select install:

Install or uninstall in Visual Studio

Now we’re done…

One thought on “C# Setup project

Leave a Reply