Archive for the ‘C# Snippets’ Category

C# Check If Application Is Already Running Then Set Focus

October 6th, 2011 9 comments

Here is a simple code snippet to assist in determining if another instance of the application is running. If there is another instance then close the current one and provide focus to the already running process.

I located this code on a forum, link here

Continue reading “C# Check If Application Is Already Running Then Set Focus” »

Passing parameters collected in dialog to Custom Action in msi C#

May 14th, 2011 Comments off

I was a little lost to why the dialog boxes I was adding to my windows installer msi was not being passed to my custom action.

There is a very simple item that I overlooked, and that is you must specify what values you want sent to your custom action.

On the properties of the custom action, there is a property called CustomActionData.

The format of this properties is:

/ParamName1=”[DIALOGPROPERTY1]” /ParamName2=”[DIALOGPROPERTY2]” …. etc

Custom Action Properties

Custom Action Properties

Then to use the parameters you will use the following code:

string sqlServer = Context.Parameters["ParamName1"];
string username = Context.Parameters["ParamName2"];
string password = Context.Parameters["ParamName3"];

C# Update app.config in msi custom action

May 12th, 2011 3 comments

I have been doing quite a lot of googling about updating the app.config file during an installation using an msi custom action. I found this blog by Rob Aquila, however this did not really work for me.

I then came across this question on StackOverflow that almost solved the problem. The key to the solution was to open the app.config and save directly to this file.

The issue I found with the later solution is that for some reason I could not get the Context.Parameters = “targetdir”. When the install was running this was always returning null.

So my solution that worked for me is as follows:

public override void Install(System.Collections.IDictionary savedState)

    var targetAssembly = Context.Parameters["assemblypath"];

    var path = string.Format("{0}.config", targetAssembly);

    var xmlDoc = new System.Xml.XmlDocument();


    var node = xmlDoc.SelectSingleNode("/configuration/userSettings/Application.Properties.Settings/setting[@name='SettingName']/value");
    node.InnerText = "PLACE NEW PARAMATER HERE";


Thank you to Khalil Dahab for his response on StackOverflow

Validate IPv4 Address

May 9th, 2011 1 comment

I’ve been looking for a very simple way to validate IPv4 addresses entered into a textbox. There are quite a few options I found, but none that really suited the simple functionality I was after.

This is the code snippet of what I have implemented:

public static bool CheckIpAddress(string addressIp)
        string[] splitIp = addressIp.Split('.');

        if (splitIp.Length != 4)
            return false;

        if (splitIp.Select(Int32.Parse).Any(result => result < 0 || 
                                                      result > 255))
            return false;
    catch (Exception)
        return false;
    return true;

“Dot” Password Character in C#

April 26th, 2011 6 comments

When you are creating a password textbox, you could use the standard ‘*’ or ‘#’, however this does not look neat nor fall in line with the windows password character.

So I prefer to use ‘‚óŹ’, to implement this use the following code snippet:

this.fooBarTextBox.PasswordChar = '\u25CF';

Converting HEX Byte String to Byte

April 5th, 2011 Comments off

If you have a Byte that is actually stored in an ASCII string for instance "5E" and are wanting this to be converted to 0x5E you can perform one simple command.

using System

public byte HexStringToByte(string hexByte)
    return Byte.Parse(hexByte, NumberStyles.HexNumber);

There is a restriction with this however, there must be only one byte in the string. This could be extended quite easily to have a byte string of multiple hex bytes and return an array or list of bytes.

%d bloggers like this: