Helpers Library


Helpers is a library that has various utility classes in it. You can see the list of utilities in the table of contents above.


Benefits of Using Helpers

Since all of the classes are fairly independent, you can take advantage of proven, working code to accelerate your development.

Sample Code

TrayForm: Minimize to Tray

This class only exists in the mpm=Silverlight binaries! Using TrayForm instead of the standard Form, allows you to to minimize to tray; it also lets you specify which icon to use. To use this class, simply subclass TrayForm instead of subclassing Form. That’s it! All you need to do other than that, is to specify the tray icon.

using CSharpCity.Helpers;

// Standard form added via Visual Studio
public partial class Form1 : TrayForm
public Form1()

private void Form1_Load(object sender, EventArgs e)
this.TrayIcon = new Icon("phone.ico");

We also expose three events you can use to trap different scenarios:

  1. OnShutDown: prior to the application shutting down by closing the form
  2. OnMinimizeToTray: when minimized to the system tray
  3. OnRestoreFromTray: when the form is restored from the system tray.

For icons, you can use Icon Finder to find decent icons. Just add the icon to your project, and specify the Copy to Output parameter as either of Copy Always, or Copy if Newer.

Finally, we provide a built-in context menu when you right-click, to give you the option to show the form, or to shutdown the application.

Pair: A Generic Pair

Pair is a generic version of the System.Drawing.Pair class, which (apart from being in a drawing-related namespace) can only take floats; the generic class lets you create a type-safe pair of any two objects.

You can create a pair like so:

Pair fullName = new Pair("Ashiq", "Alibhai");
MessageBox.Show(fullName.First); // Ashiq
MessageBox.Show(fullName.Second); // Alibhai

EmbeddedResourceFileReader: Read Embedded Files

Have you ever needed to read the contents of an embedded text file? Now you can easily do just that — read your embedded text, be it plain text, yaml, or something else. This allows you to read embedded files in Silverlight.

To use this class, first add a new text file (let’s call it helloworld.txt), and write up some content (“Hi mom!”); you also need to tell the class what your project name is, since that plays a part in finding the file. You can then access the contents like so:

EmbeddedResourceFileReader.ProjectNamespace = "YourProjectNameHere";
string content = EmbeddedResourceFileReader.ReadFile("helloworld.txt");
// ... consume content as you like

But wait! We get an exception: Couldn't open helloworld.txt. Make sure the file exists in that directory, and has Build Action set to Embedded Resource.

Ahhhh, we forgot to change the Build Action to Embedded Resource. Change it accordingly, recompile, and voila!

AudioManager (Silverlight): Play MP3s

This class only exists in the Silverlight binaries! AudioManager allows you to load and play embedded audio files. At present, it only works with MP3s.

To play a sound, simply call:


And it might play; we say might, because it will see that the file hasn’t loaded, and try to load it; if it doesn’t load in time (which it never does on the first invocation), you’ll get silence. So let’s go ahead and load the file first:

// Later

That’s better! But there are two problems here; one being that you need to doubley-mention all the paths (which means if you rename/move files, you have to change two pieces of code); and how about if you want to load 100 files?

Hence, LoadSounds loads an entire directory; coupled with a constant for the directory path, you’re golden:

const string SOUND_DIRECTORY = "Content/Audio/";
// Later
AudioManager.PlaySound(SOUNDS_DIRECTORY + "lightning-bolt.mp3");

Finally, the AudioManager cannot play the same sound file more than once concurrently; this is because the underlying MediaElement doesn’t allow it, and we can’t easily copy a MediaElement and play the copy on invoke. So if anyone knows a solution to this, let me know!

Base Converter

BaseConverter allows you to convert an integer to (and from) an arbitrary-base of characters. You can specify the characters in the base, and tada! Consider binary encoding a case where the character set is [01]; hexadecimal, similarly, is a base of [0123456789abcdef]. Here, we allow arbitrary bases.

// Convert to binary
BaseConverter.BaseCharacters = new char[] { '0', '1' };
string thirteen = BaseConverter.IntToString(13); // => "1101"
int original = BaseConverter.StringToInt("1101") // => 13

This was originally designed as part of a coding challenge where we had to store volumes of data which we referenced by ID, where we needed to compact the ID as much as possible — using a variation of Base64 (the default BaseCharacters) allows you to squeeze more numbers out of less characters (64 characters per digit instead of ten).

Hasher (Non-Silverlight)

Ever needed to quickly calculate a hash of a string? Useful for cases like uniquely identifying strings, or checking if they’ve been changed. (We used SHA-1 because it’s fast and generally “good enough” even though it can generate collisions.)

Now you can, with the Hasher class:

return Hasher.Hash("first string"); // => "31675BC1623A8B17C682839E06CCD86C10B86DE5"

Future Development

Other helper classes may be lumped together into Helpers in the future. Any helper classes that have dependencies will be split into their own libraries.

Leave a Reply

Your email address will not be published. Required fields are marked *