Tag Archives: csharp

Its time. I’m making a stand. MyNamingConventionManifesto.


Variable names, filenames and other technical names.  Hungarian notion, abbreviations, verbosity, camel case and mixed case.  How many of my projects have used mixtures of these conventions?  How often do I forget what abbrev I usd????





<rant>It is time to do away with inconsistency forever.  </rant>

I am happy to comply with the conventions of a language (eg in .net mixed case is used for public attributes and properties whereas camel case is used for parameters) or the conventions of an existing project, but if there isn’t any, MyNamingConventionManifesto comes into play.

The decisions are around:

CASE (Upper, Lower, Mixed or Camel Case) 

DELIMITER (None, Minus, Underscore or Space)



So, I hereby declare that my naming convention will not use prefixes or suffixes, neither will it use delimiters.  In short, this is:





Simple object dumping extension method for CSharp


Here’s a useful little extension method I created to dump fields and properties to the console of an object in CSharp.  

It demonstrates simple reflection and extension method syntax.



public static class ObjectExtensions
public static void DumpProperties(this object obj)
foreach (var fld in obj.GetType().GetFields())
Console.WriteLine(String.Format("{0} = \"{1}\"", fld.Name, fld.GetValue(obj)));
foreach (var prop in obj.GetType().GetProperties())
if(prop.CanRead) Console.WriteLine(String.Format("{0} = \"{1}\"", prop.Name,prop.GetValue(obj, null)));




Creating a an instance in EC2 with .net and passing parameters


When starting an Amazon EC2 instance, you can pass something called “user metadata”.  This can be a file or values.  These values are not stored in an environmental variable or file on the new instance, but can instead be retrieved by doing a HTTP Get to a “special” IP address to retrieve those values.

For example:

1.  Create the instance

RunInstancesResponse response = Client.RunInstances(new RunInstancesRequest()
  .WithUserData(Convert.ToBase64String(Encoding.UTF8.GetBytes(bootScript.Replace("\r", ""))))

2.  In a startup script on the instance, make a call like this:

1234,fred,reboot,true | 4512,jimbo, | 173,,,

See the links below for further info









Bringing a Windows Application to the Foreground using .net



It’s surprising that there’s very little information available about bringing another windows application to the foreground.

The SetForegroundWindow api call can be useful in this regard, but it can’t be used unless you know the windows handle of the main window of the application you’re activating.  If the application you’re activating has many child windows, simply using the “MainWindowHandle” property on the Process object is not enough.

The code fragment and sample will show how to find a process, enumerate it’s child windows, then send these the foreground.




using System.Diagnostics; 
using System.Runtime.InteropServices;


[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); 

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,

string windowClass, string windowTitle);

private void BringAppToForeground(string appName) 

           // Find Parent 
           IntPtr parenthWnd = GetProcessWindowHandle(appName); 
           if (parenthWnd == IntPtr.Zero) return; 
           // Get list of child windows 
           List<IntPtr> loChildWindows = GetChildWindowHandles(parenthWnd);

           // Bring Windows to Front 


private static IntPtr GetProcessWindowHandle(string processName) 
           IntPtr parenthWnd = IntPtr.Zero; 
           foreach (Process loProcess in Process.GetProcessesByName(processName)) 
               parenthWnd = loProcess.MainWindowHandle; 
               if (parenthWnd != IntPtr.Zero) break; 
           return parenthWnd; 

private static void BringWindowsToFront(IEnumerable<IntPtr> windows) 
           // Go through each and bring to front 
           foreach (IntPtr fronthWnd in windows) 

private static List<IntPtr> GetChildWindowHandles(IntPtr parenthWnd) 
           IntPtr hWnd = IntPtr.Zero; 
           List<IntPtr> loChildWindows = new List<IntPtr>(); 
               hWnd = FindWindowEx(parenthWnd, hWnd, null, null); 
               if (hWnd == IntPtr.Zero) break; 
           while (hWnd != IntPtr.Zero); 
           return loChildWindows; 

To understand how to use this in a winforms project, take a look at the sample project.

Download Sample Project