C#: Rename a project in Visual Studio

Renaming projects in Visual Studio is a pain in the @$$ and can be really time consuming if you don’t know what you’re doing. Visual Studio 2008 doesn’t come with a good refactoring tool for project renaming and the solution files can really cause problems if you don’t edit them properly.

I’ll create a somewhat fake project for this example:

Our test project
Our test project

 

When the project is created, right-click on the project and rename it from BadNameProject to GoodNameProject:

Now the project is properly named to GoodNameProject, almost. Right-click on the project and go to properties:

As you can see the Assembly name and Default namespace hasn’t change name so you need to change these as well.

Now, right-click on the project once more and select Open folder in Windows explorer, you need to rename the folder to the project as well and configure the solution file.

Rename the folder
Rename the folder

Right-click on the TestProject.sln file and select to edit it with a proper tool (such as NotePad++), you will see something like this:

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BadNameProject", "BadNameProject\BadNameProject.csproj", "{9E1B883A-025C-47AD-9350-D4CAD2760517}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{9E1B883A-025C-47AD-9350-D4CAD2760517}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{9E1B883A-025C-47AD-9350-D4CAD2760517}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{9E1B883A-025C-47AD-9350-D4CAD2760517}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{9E1B883A-025C-47AD-9350-D4CAD2760517}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal

Change references from the old BadNameProject to the new GoodNameProject.

When you return to Visual Studio you will be prompted to take action against the outside changes to the solution file. If you want to save your old project do so, but Discard is what you want to choose here and embrace your new changes!

 

Tadaa, you’re pretty much done. Depending on how much refactoring you need to do of course, don’t forget to change the project namespaces in your files…

Tip: If you have to change a lot of references from the old namespace to the new, use the SHIFT + CTRL + F12 debugging tool (jump to next error) to quickly add new references.

Adding comments to custom controls in the win form designer

As I was making a custom win form control I saw that my standard XML-comments wasn’t showing for my events in the win form designer. As I don’t use win forms that much I was a bit baffled, but thought that it ought to be possible to display standard help information just as the normal native controls do on their events.
And it was, but googling the issue was hard.

You have to use attributes to attach the information to your control or event in order for the Visual Studio designer to care. The attributes allows you to add a comment, a category and more to better display your component in the designer, here’s an example of an event with a comment that displays in the designer:

[Description("Occurs whenever a user changes date")]
public event MonthPickerChangeHandler Change;

This will display in the designer like this:

The comment is displayed in the designer
The comment is displayed in the designer

A list of the various attributes to use can be found here: http://msdn.microsoft.com/en-us/library/tk67c2t8(v=vs.100).aspx

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…

The difference between build and rebuild

Sometimes you wonder about the stuff that resides in the Visual Studio menus. For example, what’s the difference between Build and Rebuild, and when do you you which one?
I haven’t really thought about it, to be honest, but I’m currently working on a somewhat gigantic project and it takes a noticeable time to build, and that’s when it’s important to know the difference.

Build:
Ctrl+Shift+B
Builds all files that has changed since the last build and is thus faster than Rebuild.

Rebuild:
Builds all files in your solution (if you Rebuild on solution level) or in your project (if you Rebuild on project level) regardless of whether or not that have changed. Sometimes you need to run a Rebuild when Visual Studio bugs out since Rebuild removes all previously existing build-generated files.

C#: backtracking while debugging

Sometimes when you debug you realize that it would be very handy to step back a couple of lines. Until recently I didn’t know how to do this (not something you need very often admittedly) without restarting the application, so I thought I’d put it up here in case you’ve missed it as well.

When you debug you can right click on a line you’ve passed and select ‘Set next statement’ which allows you to go back to that row and continue debugging from there:

Välj 'Set next statement' Choose ‘Set next statement’

 

Vips så är du tillbaka
And you're back!

Do however have in mind that when you go back the variables are already set which might have effect the values if you continue. In my example the values are not effected but if you, for example, increment a value it could already have incremented a couple of times which will result in an unexpected behavior. You’re not going back in time, just back in code.

This can also be useful if you want to jump passed code or take a short-cut in a if-statement which you might not enter otherwize. Set next statement works both forward and backwards!

C#: conditional debugging

Sometimes when you loop through large amounts of data you want to add a breakpoint which won’t necessarily stop in every loop but rather when a specific value pops up. One way is to put an if-condition in the loop and put your break point there instead, but that would actually force you to alter your code just to debug.

In Visual Studio (2008 at least) you can add so-called “conditional break points”. These allows you to set a condition for the break point which must be met. This is very simple and I’ll show you how:

Create an enormous and utterly pointless loop, add a break point in the loop and then right click on that break point:

Villkorad breakpoint
Press 'Condition' to open the next window

 

Skriv in ett booleanskt uttryck
Add a boolean expression, ctrl+space still works for intellisense

In our case we are incrementing the variable ‘i’ and we want to pause when the value hits 39 so we add that as a condition. Then we run the loop and wait for it to stay:

Debuggern stannar på vårat värde
The debugger has stopped!

My proof of this working is that I wouldn’t sit and press next 38 times just for a blog entry!