January 30, 2011

C# - Project Properties With Example

Source Code : Download

1. Introduction

Here, I will explain some important project properties with examples. I will show you the following in this article:

  1. How do you set the Output path for your binaries?
  2. How do you set version and other file information to your output binary?
  3. How do you set Icon to your project?
  4. How do you conditionally compile some piece of code?
  5. How do you ask the compiler to skip some warning or treat a warning as an error?
  6. How do you use Pre-Build and Post-build events?
  7. How do you debug a dll and get a hit to the break point when the when the dll project in not part of the solution in which exe resides?

Hope, most you know all or some part of it. You can jump to separate section if you need. Let me start one by one. Before that, we need some preparation. That is we need some projects and code to explore the above said stuff.

2. Prepare the Projects

First open two instances of the visual studio 2005 and create a new Visual C# windows application in one VS2005 and Visual C# Class Library In the Other Vs2005. Now we have two projects and they does not belong to the same solution as you created it two separate VS2005 instances.

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

//001: Function added to add to numbers and
// Return a value
public int add_two_Numbers(int first, int second)
    //002: To hit the break point added some local variables, performed
    // calculation on the local variables
    int n1, n2;
    n1 = first;
    n2 = second;
    return (n1 + n2);

If I explain the above function in a big paragraph, I know you will definitely beat me J

Anyhow, I can say, this function exposed by our dll and it is going to be used by the Windows application.  I purposefully kept the local variables just to show you debugging and get a hit on the breakpoint.

4) Go to the "Windows Application" now
5) Design the form as shown below and set the properties by referring the attached sample application

3. Setting Output for the Projects

Now we have two separate projects. One will deliver the dll and the other one will deliver the exe. But, our exe is going to use dll to calculate the addition of two numbers. In Real world scenario, these projects will be grouped under single solution and build order is set to dll first and exe next.

Now we will do that manually. That is, whenever you do build, first build the Class library project and then exe.

1) Create a folder called output in your 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 you in step 1 using the folder browser button. The screenshot below shows the default path and you are overriding that default path to C:\Output. Now build the project and make sure the dll is delivered to C:\Output folder

4) Go to the Windows application project
5) Click on the Project | Add reference
6) Click on the Browse tab on the displayed dialog
7) Select the Dll delivered by the Class Library project and press the OK button.
8) Now add the following piece of code:

A: Using statement for the form

//000: Using the Class Libraray Dll
using SampleMath;

B: Add the following code on the Button event handler:

//001: Use the class from our library
SMath obj = new SMath();
int n1, n2;
string result;

//002: Call the function and display the result
n1 = int.Parse(txtNumber1.Text);
n2 = int.Parse(txtNumber2.Text);
lblResult.Text = obj.add_two_Numbers(n1, n2).ToString();

In the above code, we are using the class SMath exposed by our class library and making the call to the add_two_numbers. However, this is not the purpose of this article. You now already know how to set the output path. So, set the same output path for this Project also. Build the project and make sure the Exe is delivered to the same output path. This is how large solutions with plenty of projects in it deliverers the binaries to a single location.

4. Set Version and Other File Information

In the previous section, we set the binary delivery path to a single location. And, both the projects deliver their output to the C:\Output. Now we will set the version information for the Dll that will be delivered by the Class library project. 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
  5. From the dialog displayed fill the information as shown below:

There is no explanation required on the above dialog as displays basic information.

6) Now re-build the Class Library.
7) Place the cursor on the dll name from the output path. You will get tool tip of the information that was set in the dialog shown above. Below is the screen shot of the tool tip

Oops. The screen shot of the dialog is old. I do set Company as C-Sharpcorner.com but taken an old screen shot. OK. You can also see more detailed information by viewing the file properties. The second tab (version) has all the information we assigned. Do the same steps for the exe application also.

5. Setting Icon to the Exe project

Let us set the icon for our windows application project. Given below the steps required for it:

  1. Bring up the Project properties
  2. Select the Application tab
  3. In the resources group, there is radio button called Icon. Check the radio button and select an icon file using the folder browser dialog.
  4. Rebuild the Exe project. The exe file in the output folder now has the selected icon.

6. Conditional Compilation

Conditional compilation means compiler makes a decision whether it needs to compile some set of code or not. This kind of compilation can be done by marking the piece of code with a tag. Consider our exe application. When we click the Get Result button we do get the addition of two numbers in the result. Let is add the exclamation symbol at the end of the result based compiler tag.

First, add the following piece of code in the button click event handler at the end:

//003: These statements are conditionally compiled
#if COND
lblResult.Text += " !!!";

The COND is the tag that we can specify as a project property to make the value of COND to true. When the compiler translates the code it sees the #if evaluates to true and translates the code lblResult.text += “!!!”

Follow the below steps to inform the compiler to translate 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 as shown below:

  1. Rebuild the solution
  2. And double click the exe.
  3. Now the addition will give the result with three exclamation symbol added at the end
  4. Now remove the COND from the above-shown dialog and rebuild the project. Run the exe and make sure exclamation not appear when you test the result.

The “#if” statement can be used anywhere in the project source code in multiple places with a tag to perform conditional compilation.

7. Adjusting the Warning level

When you compile the projects you may see some warning reported by the compilers. Warnings are not Errors. The compilers still do their job even though a warning is reported. So there is a way to inform the compiler to say, “You can skip this warning. I am not interested in it” or  “Hey. Treat this warning as an error. I (Developer) pay more interests to this kind of warnings”.

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

//001: Use the class from our library
SMath obj = new SMath();
int n1, n2;
string result;

//002: Call the function and display the result
n1 = int.Parse(txtNumber1.Text);
n2 = int.Parse(txtNumber2.Text);
lblResult.Text = obj.add_two_Numbers(n1, n2).ToString();

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

The issues warning number is 168. Once you have this, you can inform 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.

3) Rebuild the project and see what happens. The compiler will inform variable not used as an error.

4) You skip this warning by placing the same number 168 in the text box "Suppress Warnings"

8. Pre-build and Post-build events

These are kind informing what should happen before the build starts and what should happen when it ends. People usually go ahead do some file copies 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 the commands that help the build process.

  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 real world, if you perform some file copy in the pre-build, the build will wait for the operation to be completed. Once the build process is completed a calculator is displayed.

9. Debugging the Dll

Do you remember? We created the class library as a separate project. We can easily debug the dll when the exe project and the consumed dll both are in the same solution. If the dll is a separate project, like that in our case, we need a hosting exe to debug the dll.
  1. Go to the class library project. You cannot directly run this project, as the output is not an executable.
  2. Open the Project properties page
  3. Under the start, action browse and select our Windows Application project exe to populate the start external program text box. This tells that the exe uses the dll and the exe can be used to load the translated for the dll.
  4. Put a break point on the add_two_Numbers function and click the run button to get a hit on the break point.

Note: The sample is created in the VS 2005 IDE. If you have the latest version say yes to the conversion dialog box

Source Code : Download


Leave your comment(s) here.

Like this site? Tell it to your Friend :)