Tag Archives: csharp

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

Standard

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????

?!It_never_ends!?   

no-longer   

I-AM-SICK-OF-IT!

_this_has_to_stop

<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)

PREFIX

SUFFIX

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

MyNamingConventionManifesto 

 

ThankyouThatWillBeAll

 

Advertisements

Simple object dumping extension method for CSharp

Standard

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)
{
Console.WriteLine(obj.GetType().Name);
Console.WriteLine("{");
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)));
}
Console.WriteLine("}");
}
}

 

 

 

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

Standard

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()
  .WithImageId(ami_id)
  .WithInstanceType(instance_type)
  .WithKeyName(YOUR_KEYPAIR_NAME)
  .WithMinCount(1)
  .WithMaxCount(max_number_of_instances)
  .WithUserData(Convert.ToBase64String(Encoding.UTF8.GetBytes(bootScript.Replace("\r", ""))))
);

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

GET http://169.254.169.254/latest/user-data
1234,fred,reboot,true | 4512,jimbo, | 173,,,

See the links below for further info

 

 

 

 

 

Links

http://stackoverflow.com/questions/7420368/how-to-start-an-amazon-ec2-instance-programmatically-in-net

http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html

Bringing a Windows Application to the Foreground using .net

Standard

image

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.

 

BringAppToForeground(“notepad”)

………

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

……….

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

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 
           BringWindowsToFront(loChildWindows.Reverse<IntPtr>());

}

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) 
           { 
               SetForegroundWindow(fronthWnd); 
           } 
}

private static List<IntPtr> GetChildWindowHandles(IntPtr parenthWnd) 
{ 
           IntPtr hWnd = IntPtr.Zero; 
           List<IntPtr> loChildWindows = new List<IntPtr>(); 
           do 
           { 
               hWnd = FindWindowEx(parenthWnd, hWnd, null, null); 
               if (hWnd == IntPtr.Zero) break; 
               loChildWindows.Add(hWnd); 
           } 
           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