Tag Archives: C#

Get local machine’s MAC address in C#

Here’s a way to get the local machine’s MAC address in C#. Note that there may be various MAC addresses (Ethernet cards, local loopback devices, hooked up 3G devices etc.), so we try to find only the Ethernet MAC address:

static string GetMacAddress()
{
  string macAddresses = "";
  foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
  {
    // Only consider Ethernet network interfaces, thereby ignoring any
    // loopback devices etc.
    if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
    if (nic.OperationalStatus == OperationalStatus.Up)
    {
      macAddresses += nic.GetPhysicalAddress().ToString();
      break;
    }
  }
  return macAddresses;
}

Inger Programming Language

Back in 2003, a small team of students in the Netherlands worked on a new esoteric programming language called Inger. It was modelled after C, but with some syntactic sugar that made it more readable:

  • Inger forces braces for every block statement
  • The parameters and result of a function are specified in funcional notation:
sum: int x int y -> int
{
  return x + y;
}

Inger was created specifically to accompany the book “Compiler Construction – A Practical Approach” which discussed the development of a compiler from the ground up.

The open source project is still available at Sourceforge here. Also, the book may be downloaded from the Inger project page or, more simply, here (PDF, 2.14 MB).

A Word Search Tool for Creating and Solving Puzzles

crossword
Crossword

I wrote a little tool today that others may find useful. This tool allows you to load a list of words (could be a big list, like a dictionary), and then search it using a regular expression. I needed it to help me create some puzzles (letterfind, swedish and crossword styles).

The program was written in C# and requires .NET 2.0 on your machine. It comes with two sample word lists: one with almost a million words from the Portuguese language, and one with 50 names of trees in Portuguese (of course the program can handle words in different languages!).

Be sure that your wordlists are plaintext with one word per line, and in UTF-8 format, e.g.:

desk
computer
mouse
blackboard

See the included word lists for samples. The alphabetic order in the file doesn’t matter. The program will ignore diacriticts in its searches (i.e. acção = accao). You can use wildcards like % and . in your searches:

  • %cao” returns everything that ends in “cao” (or ção, since diacritics are ignored)
  • “a..ba” returns everything that starts with “a”, then two random letters, and ends with “ba”
  • The query “….” returns all four-letter words.

Here’s the tool for download: Dict.zip (2.1 MB).

If you don’t have the .NET framework installed, you can get version 2.0 here (higher versions will also work – I tried to keep the requirements low).

As a bonus: this (PDF, 286 KB)  is something that was made using with gathered using this tool.

Free Charts Library Development

It’s happened a number of times now that I’m writing a piece of software that needs charts of some sort. Sometimes I’ll need a line chart, sometimes a bar chart, a pie chart, maybe even a Gauss diagram. There are cases that I’ll need a tiny chart that’s not too accurate but shows a tendency, shown as a bitmap (what seems to be called “sparklines”), and sometimes I’ll need a detailed line chart of visitors to a site by month, or number of pushups by day for a member of a gymnasium, or even a world map with data by country. I’ve done projects needing such charts in C#, in PHP, and in Flash.

There’s a problem: there doesn’t seem to be any (quality) free charts package out there. Good packages for PHP are commercial (i.e. JpGraph), but there’s open source stuff available (e.g. pChart) but this is obviously not portable to C#, or Flash. The Google Charts API seems to be the most flexible thing out there, but obviously it’s only available on the web and only for sites that are actually online, i.e. not for intranets. Yet Google Charts is exactly what I want: well thought out, easy to use, very flexible.

Since the backbone of a charts library is some math that allows us to calculate the axes, projection of points on the chart, creating the legend etc., it should be written in a portable way so that it becomes available and reusable in multiple programming languages. Google’s Charts API could be the basis for how charts are specified.

What I’d really like to do is write the algorithms to produce various chart types in pseudocode, so that they could then be implemented in various programming languages, possible then using the Decorator pattern to add features that are language-specific (lots of interesting things can be done with Flash, like mouseovers and zooming).

This’ll be a pet project for a while…

Expandable Properties in Visual Studio .NET

I had written a control with a number of properties, most of which showed up automatically in the Visual Studio Properties window. Simple data types were not a problem, nor were arrays: Visual Studio automatically opens up a window that lets the user edit the array elements. However, class or structure instances were not editable out of the box: they were shown grey-out in the Properties window.

The solution to this lies in that Visual Studio needs to know how the subclass or subproperty should be edited. An example of this is the Padding property that all controls share: it’s a structure with a couple of properties (Left, Right, Bottom, Top and All), and in the Properties window you can expand the Padding property so you can edit any of these properties individually. This is precisely what I wanted for my substructures.

It’s easy to convince Visual Studio .NET to give you this behavior by adding a TypeConverter attribute to your structure, like so:

[TypeConverter(typeof(ExpandableObjectConverter))]
public struct MyStruct
{
  ...
}

ExpandableObjectConverter is already implemented for you, as are some others. If you want different behavior in the Properties window, you can implement your own TypeConverter.