Skip to content

FAQ on C-Sharp Project Properties

1. Introduction to C-Sharp Project Properties

In this Article, we will explore some important project properties with examples. We will cover the following FAQ when we are proceeding with our exploration. Before that, we need some preparation. Means, we need some projects and code to explore the project properties.

2. The MathUser & SampleMath Projects

First, we need to open two instances of the Visual Studio 2005. In one instance, we create a new Visual C# Windows Application called ‘MathUser’. And in other VS2005, we create a C# Class Library Project called ‘SampleMath’. Now, we have two projects and they do not belong to the same solution as we created it in two separate VS2005 instances.

2.1 SampleMath Class Library Project

First, let us deal with the Class Library Project SampleMath:

  1. Go to the ‘Class Library’ project.
  2. Change the class name to ‘SMath’.
  3. Add the following piece of code.

The above function is exposed by our ‘SampleMath.dll’. The function adds two numbers and returns the result to the caller. We kept two local variables n1 and n2 in the function. We need them later while we explore the C# debugging project properties in the later part of this article.

2.2 MathUser Windows Application Project

Now, let us deal with ‘MathUser’ Windows Application project. Design the form as shown below and set the form properties by referring the attached sample application.

CSharp MathUser Windows Project
CSharp MathUser Windows Project

We have our projects ready to deal with the Frequently Asked Questions (FAQ) on C# Project Properties. Let we explore it one by one.

3. How Do I Change Build Output Path?

Now we have two separate projects. One will deliver the Dynamically Linked Library (DLL) and the other one will deliver the Executable (EXE). Our EXE will use DLL to add two numbers. When a single Project Team handles both the projects, these projects will be grouped under single solution and the build order is set to DLL first and EXE next. But, when a third party develops the DLL, the team developing the EXE refers the DLL as binary.

Here, we will take the second approach. This means, whenever we build, we first build the Class library project and then the EXE project. Now, we will change the default Output Path project property for our Class Library Project. Follow the steps given below:

  1. Create a folder called output in ‘C:’ using windows explorer.
  2. Go to the Class Library project, move to Solution Explorer and double-click the properties icon which is usually above the references icon.
  3. Select the output path to the folder created by us in step 1 using the Folder Browser button. The screen-shot below shows the default value for the Output Path Project Property. We override that default path to C:\Output. Now build the project and make sure Visual Studio delivers the DLL to C:\Output folder. The below picture shows how to set the Output Path Project Property :
Set C-Sharp Project Property for Binary Output Path
Set C-Sharp Project Property for Binary Output Path

4. How Do I Add DLL Project Reference to My Project?

Now, our class library project delivers the DLL output to our desired location. We will make the Windows Form Application project to refer the DLL produced in the previous step.

4.1 Steps to Add Reference

To add a reference to a project, follow the steps below:

  1. Make sure Windows application project is opened in the IDE.
  2. Right click reference node and choose ‘Add reference…’ option.
  3. Click on the Browse tab from the displayed dialog.
  4. Select the DLL delivered by the Class Library project and press the OK button.

The below picture shows the steps:

dd DLL Reference To A CSharp Project
dd DLL Reference To A CSharp Project

4.2 Call The Function From Class Library

Now, MathUser Windows Application Project is referring the Dynamic Linked Library built by the Class Library Project. We can now call the functions exposed by the SimpleMath DLL. We will call the DLL function in the Windows Form when the user clicks the Get Result button. So, first we should add a using statement in the C-Sharp file which belongs to the form. Below is the code:

Next, we add the below code for the click event handler of the Get Result button.

In the above code, we are using the class SMath exposed by our class library and making a call to the add_two_numbers function. The Label Control in the form displays the returned result.

We already know how to set the Output Path Project Property. So, we have to set the same output path for this EXE Project as well. Then, we build the Project and make sure the project delivers the EXE to the same output path which we set for the Class Library Project. This is how large solutions with plenty of projects in it deliverers the binaries to a single location. Moreover, binaries in one location helps us to launch the EXE without coping the dependent DLLs. In our case, when we launch the project by double clicking the EXE, the EXE gets launched with no error as the dependent DLL is in the same path

5. How Do I Set File Version & Assembly Information?

In the previous section, we set the binary delivery path to a single location. This way, both the projects deliver their output to the location, C:\Output. Now, we will set the version information for the DLL. Follow the below steps for setting the version information for the DLL. You can use the same steps to set the version information for the EXE project.

  1. Go to the solution explorer view of the class library project.
  2. Double-click the Properties as we did previously.
  3. Click the Application tab on the left side.
  4. Click the Assembly Information… Button

The below picture shows the steps:

Setting C-Sharp Project Property, Assembly Information
Setting C-Sharp Project Property, Assembly Information

Assembly Information is useful for an end-user to know the publisher of their deployed C# binaries. When there are many binary files, they can check publisher, version information etc., to claim those are in sync. The above steps will open an Assembly Information Dialog. In the dialog, we can fill the details as shown in the below picture.

Assembly Information Dialog
Assembly Information Dialog

In the Assembly Information Dialog, one can mention the Publisher Information, Copyright, Trademark and Version Information. Now, we can rebuild the Class Library to deliver the new binary to C:\Output. When we place the mouse cursor on the file name, the tool-tip will display the Assembly Information. The details tab of the File Property Dialog shows the same information. Below is the screenshot of both:

Assembly Information Seen By End-User
Assembly Information Seen By End-User

6. How do I set Icon to My C-Sharp ‘EXE’?

In Windows Explorer, we can see every file has an icon associated to it. Our Mathuser.exe will have a default icon dedicated for an EXE file. Let us change this default icon for our windows application project. Follow the steps below to set an icon to the project:

  1. Bring up the Project Properties.
  2. Select the Application tab.
  3. In the ‘Resources Group’, there is a radio button called ‘Icon’. One can check the radio button and select the icon file using the file browser dialog.
  4. Rebuild the EXE project. The EXE file in the output folder now has the selected icon.

The below picture shows setting the Icon Project Property:

Setting C-Sharp Project's Application Icon
Setting C-Sharp Project’s Application Icon

The below screenshot shows selected icon displayed in front of the EXE name.

Custom Application Icon For the C# EXE
Custom Application Icon For the C# EXE

7. How Do I Perform Conditional Compilation?

When the compiler decides whether it needs to compile a set of code or not, we call it as Conditional compilation. We can do this kind of compilation by marking the piece of codes with a tag. In our ‘Mathuser.exe’ when we click the ‘Get Result’ button we get the addition of two numbers in the result. Let we add the exclamation symbol at the end of the result based on the Compiler Tag. Now, add the following piece of code at the end of the button click event handler.

The COND is the tag which surrounds the piece of code within a pair of #if and #endif. It is a simple ‘If Condition’ for the compiler and when the COND evaluates to true, the compiler compiles the code which lives between the conditional construct. In our case, it compiles code which adds the exclamation string at the end of the Label. We can set this tag as Project Property and in the subsequent compilation, the compiler will evaluate the COND as true.

Follow the below steps which will inform the compiler to compile all the code tagged by COND:

  1. Bring up the Project properties page.
  2. Go to the Build tab.
  3. Type COND in the Conditional Compilation text box.

The below picture shows these steps:

Setting Conditional Compilation For CSharp Project
Setting Conditional Compilation For CSharp Project

Test the Application

Now we can Rebuild the project and double click the generated EXE. When we click the Get Result button, the application shows the added result with trailing exclamation symbol. Since, the COND is true during the compilation process, the generated EXE has machine code which adds trailing exclamation symbol. If we remove the COND from the Build Property, the generated EXE will not show this exclamation as compiler skips the tagged code.

Note:

A developer can use ‘#if’ statement anywhere in the project n number of times. One example is, generating code for special debugging or logging.

8. How Do I Adjust Warning-Level Through Project Properties?

When we compile the projects, we may see warning reported by the compilers. The compilers still do their job even though a warning is reported. So, there is a way to inform the compiler to skip the warnings or treat warnings as errors.

Note:
Warnings are not Errors.

Consider the below piece of code which is taken from the button click event of our windows application:

Here, the variable result is declared and not used. The compiler will give  warning on this as shown below:

MathUser\MathUser\frmMathUser.cs(25,20): warning CS0168: The variable ‘result’ is declared but never used

The compiler issued warning number is 168 (cs0168 taken from the warning message). Once we have this, we can inform the compiler either to skip this warning or treat this warning as an error. Follow the steps below to treat this warning as error:

  1. Open the project properties.
  2. In the build page, Under ‘Treat Warning as Error’ group, Select ‘Specific Warning’ radio button and place the number 168 in the input box.

The below screenshot shows these steps:

Csharp Project Property 'Treat Warning As Error'
CSharp Project Property ‘Treat Warning As Error’

After setting the project property, we must build the project. This time, the compiler will inform variable not used as an error as shown in the below screen-shot:

Warning Treated As Error
Warning Treated As Error

Note:
You skip A warning by placing the warning number in the ‘Suppress Warnings‘ Project Setting

9. How Do I Invoke External Command Before And/or After the Build?

Invoking the external command before or after the build process can be achieved through the ‘Pre-Build’ and ‘Post-Build’ events. These are informing what should happen before the build starts and it ends. People usually copy files on these events. To explain the usage of these events, we will display a notepad before the build starts and display the calculator when the build is finished. But, in the real world, it should be the commands like ‘XCOPY’ that helps the build process. Follow the steps shown below:

  1. Bring up the project properties for the windows application project and click the build events tab.
  2. Type notepad in the ‘Pre-Build’ text box and ‘calc’ in the ‘Post-Build’ text box.
  3. Start the rebuild and see what happens. Also, note that the build process waits until the notepad is closed. So in a real world, if we perform some file copy in the ‘Pre-Build’, the build will wait for the operation to be complete. Once Visual Studio completes build process, C# displays a calculator.

The below screen-shot shows setting these project properties:

Set Csharp Project Property for Build Events
Set CSharp Project Property for Build Events

10. How Do I Debug A DLL Project?

Remember, we created the class library as a separate project in a separate solution. It is easy to debug the DLL when the consuming EXE project lives in the same solution. If the DLL is in a separate project, we need a hosting EXE to debug the DLL project. Follow the below steps useful to debug a DLL project without having the source code for consuming EXE:

  1. Go to the class library project. We cannot run this project as the output is not an executable.
  2. Open the Project properties page.
  3. Select the Debug Page.
  4. Under the ‘Start Action’, select the ‘Start External Program’ radio box. Then using the browse button, we need to select our EXE from the output folder. Now, whenever we ask to run the DLL project, it first starts the EXE which consumes this DLL.

The steps are shown in the below screen-shot:

Set CSharp DLL Project Start Action
Set CSharp DLL Project Start Action

Now, we can put a break-point on the ‘add_two_numbers’ function. Starting the DLL project in Visual Studio IDE will launch the MatchUser.EXE and our break point hits when we click the Get Result button in the EXE.

Code Listings

Project: SampleMath
Code Listing1: SMath.cs

Project: MathUser
Code Listing2: frmMathUser.cs

Source Code : Download From Google Drive

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: