Tuesday, May 6, 2008

Tip: Remove the hidden .svn folders

Are you using subversion as your source code repository? Do you want to delete all the .svn hidden folders inside your project folder? Last year, I got the solution I required from Jon Galloway's blog @ here


Initialization code for log4net in c# web service project

log4net is the .NET version of the Java logger, log4j. I have used it in both .NET Full and .NET CF Framework projects. Now, I have developed a sample web service and successfully configured the application to use log4net. The one tricky part is to correctly tell the application to load the configuration settings when it starts.

The initialization code can be put in the the web project's Global.asax.cs:

The Application_Start method is called automatically when the web service is loaded, and thus, it is the ideal place to initialize the log4net environment.

More info on log4net

Friday, May 2, 2008

Creating shortcut for Add/Remove Programs window

Here's a cool tip to directly open the Add/Remove Programs window without going through the Control Panel window.

Type control appwiz.cpl in the Run command window and click OK.

In my case, I needed a shorcut the desktop to the Add/Remove Programs and here are the basic steps:

1. Right-click on the desktop
2. Select New -> Shortcut
3. In the Create Shorcut window, enter control appwiz.cpl and click Next.
4. In the Select a Title for the program, enter a descriptive name and click Finish.

And if the shortcut icon is a big ugly, then you can change it as well from the shortcut properties window.

Escape ampersand (&) character in C#

Set the Text property of a Label with a string literal having ampersand (&) characters. When the application is run, the ampersand character is not displayed in the label.

The default behaviour of Labels interprets ampersand characters (&) in the Lanel control's Text property as an access key prefix character, and not as a character literal.

To escape the ampersand character, I will suggest two ways.

1. Use double ampersand. However, this modifies the original string literal, which you probably don't want.

2. Set the Label.UseMnemonic Property to false (since by default, value for the UseMnemonic property is true).

More information on Label.UseMnemonic Property on MSDN

Thursday, May 1, 2008

Removing InstallState files after uninstallation

Once everything from the previous post is working, one potential problem that can be encountered is that INSTALLSTATE files are not removed after uninstallation of a .NET Framework 2.0-based application. This is an issue related to custom actions and the following article from Microsoft gives a very good description of the exact problem:

An .installstate file is not removed after you uninstall a .NET Framework 2.0-based application

Since the Workarounds suggested in the article didn't make much sense to me, I have found another way to solve it.

the same custom action (that was initially used for Install custom action) to the Uninstall custom action.

Then, Override the Uninstall method in the installer class and make sure it calls the base Uninstall method.

Update application config file during installation process

I was looking for sample code snippet(s) in the MSDN Library that allow the application's configuration file to be modified during the installation process. I've found the following article:

Redirecting an Application to Target a Different XML Web Service During Installation

That's exactly what I needed and it just took me just a couple of minutes to code it in C#.

I think it won't be a big deal to complete the last part ... :-) Is it?

SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.

SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.

Problem Description
I encountered this error when attempting to start a project's exe file compiled under the Release configuration.

Analysis & Observations
The exe file compiled under the Debug configuration started without any problem and there were no error messages or exceptions.

Starting the Release version from Visual Studio (though F5 - start debugging), it worked fine as well.

The Main() function had the Application.SetCompatibleTextRenderingDefault method call in the second line before the Load method call. So, this should be working fine.

The application configuration file has been renamed deployed.

When starting the exe, the process shows for a second in the Task Manager and then disappears (which is not what I expected).

There is no pragma code or conditional compilation sections in the code that may not execute when running in Release Mode.

Problem Cause
After all these observations and some further investigation in the code, I have finally found the real problem. The MainFormController is a singleton and had a Form as an instance member. This form was being instantiated when the class was being loaded in the memory, well before the Application.SetCompatibleTextRenderingDefault and Load method calls.

I have changed the code to lazy initialization of the form, and thus, the form will be instantiated only when it is requested for the first time. Wohoooooo.