WMI (What Magnificent Information)

Yesterday I blogged about a class I came across in the SystemInformation namespace called PowerStatus. It was working great for me until I tested it on a machine with three UPS's plugged into it. The three UPS's were not for that machine of course :) they were in a rack with a bunch of controllers etc. We wanted to monitor all three UPS's and find out how much life was left in the weakest, so we can shut down the system and save all data before any of them go.

The problem with Power Status was it seemed to give me an average of all three batteries and on my laptop it got nothing. So I asked my network of developers and Andrew pointed me to WMI (Windows Management Instrumentation). Well there is a wealth of information in there. And it can all be called nativley from .Net. I thought I would post some sample code. Gives me a place to come look for it next time I need something similar.

 This code gets a collection of Batteries from the current machine and fills a listbox with their Names and remaining life.

ConnectionOptions options = new ConnectionOptions();

options.Impersonation = ImpersonationLevel.Impersonate;

options.Authentication = AuthenticationLevel.Default;

options.EnablePrivileges = true;


ManagementScope connectScope = new ManagementScope();

connectScope.Path = new ManagementPath(@"\\" + Environment.MachineName + @"\root\CIMV2");

connectScope.Options = options;



SelectQuery msQuery = new SelectQuery("Select * from WIN32_Battery");

ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(connectScope, msQuery);



foreach (ManagementObject item in searchProcedure.Get())


    this.listBox1.Items.Add(item["DeviceID"].ToString() + " - " + item["EstimatedRunTime"].ToString());


Notice the query in the middle of this code that is looking in Win32_Battery click here to see what else you can query out there.


The project I am working on right now is a software factory that will eventually allow it's users to build software that controls robots and other devices in a laboratory.

Today I wrote a service that monitors the power on the machine it's running. The idea is the service will raise an event if the machine loses power, and will then monitor the remaining life of the UPS. This will allow the system to save any data to disk before the UPS gives up the ghost.

To write this I used a class that is new to .NET 2.0 called PowerStatus. This is a very useful little class.

First of all you can check various useful properties relating to your machines power supply.

Also you can handle the PowerLineStatusChanged event which will fire when the Power State changes.  In this event you can tell if the Power is connected or not and if the computer is about to resume or suspend.

There is a great code example and sample application here



LEAD Technologies Inc. V1.01

VSTS ClassCleanup Method

A colleague and friend of mine Andre who in my opinion is a guru of Test Driven Development discivered this issue.

If you're used to Nunit and how it fires methods in your test Fixture you may assume VSTS fires the equivalent methods in a similar way. Well think again. I am specifically talking about the method you decorate with the ClassCleanup attribute.

Lets assume you have some code in your ClassSetup and ClassCleanup methods you would expect it to setup and cleanup for all the tests in that class. According to the comments generated by MS when a test class is created automatically, you would assume these methods execute before the first test runs and after the last test in that class runs.

Here is the comment you see in the Additional Test Attributes Region of a auto generated Unit Test.

//Use ClassInitialize to run code before running the first test in the class
//Use ClassCleanup to run code after all tests in a class have run

The Comments seem clear enough, or do they?

The Nunit equivalents would fire in the order you would expect from the above comments:



In VSTS they fire in this order:


Do you see the difference? The cleanup from class1 is not done until after class2 tests have been executed. All the class cleanups are done at once.

What if I wanted to set something up and tear it down for each class? Do I have to keep track of what order people will be running the tests and make sure I perform the setup only once in the first class to execute a test. What if I only want to run the tests in Class 2.

To me this is a design error, intentional or not.


Have you been looking for Ndoc or the cool document generator that was available for C# in VS 2003. They no longer seem to exist, I'm not sure what the whole story is behind the scenes, but I have come across a nice tool that works in their place.  

It's called SandCastle. I checked it out and it worked very well, creating some really nice MSDN stlye documentation complete with C#, VB.net and Managed C++ samples. I tried out Eric Woodruffs GUI which was easy to use especially if you've ever used Ndoc.

Give it a try I think you will be impressed with the outcome.


LEAD Technologies Inc. V1.01

TFS Time Entry Utility

Many clients ask me if there is a tool that will help you keep track of time spent on different tasks in a TFS Project. The answer has always been, "Not that I have seen, but it wouldn't be hard to write one".  

Well I put my code where my mouth was and I wrote this little utility, of which I am happily sharing with anyone who would like it. It's a windows application that sits in your system tray when minimized and allows you to select a project, query and workitem, hit start and work away on that work item. When you hit stop it will add the amount of time you spent to the Completed work field and subtract that same amount of time from the Remaining work field of the same work item.

I want to change the UI but have not had a chance yet. I would also like to split the code up and get it out of the window. I would also like to get the old water heater out of my basement and to the dump but there is always something more important to do.

I have added a project to CodePlex so if anyone wants to work on it please let me know and I will add you as a developer.

Meanwhile your feedback is very welcome.

Some enhancement ideas I have:

  1. Set the Project and Query off the right click on the system tray (similar to the Server)
  2. Make the list of work items a long list so you can just click on the item to start timing. (Or perhaps a clock next to it)
  3. Make the fields it updates configurable in case you have customized the WorkItem.
  4. Configiure the Work Item types that should show in the list. For now the Query can do that.
  5. Make it an add in for VS
  6. Add it as a control to the Work item field it self.



Junk Mail

I have posted before about Junk Mail and how I am inundated with it in so many forms, one of which is in the mail box at our house. Some days you get the mail out of the mailbox and find nothing but a bill and 7 flyers for windows, pizza and credit cards.

While chatting with a friend of mine about this he gave me this great idea. I have done it about 5 times now. I'm not sure what effect it will have on the companies who send out this crap but it makes me feel better.

More then once a week I receive a letter from some credit card company with an application form for their credit card. Included in the main envelope is postage paid envelope that I am supposed to use to send back my application form. Instead, I take all the junk mail I received that day and stuff it into this envelope seal it up and mail it back to them.

 I hope they are enjoying the Pizza coupons and window adds.



LEAD Technologies Inc. V1.01

MS Project and Work Items

If you are a project manager or know a project manager who is using Microsoft Project with Team Foundation Server to manage Work items you may have noticed that Start and Finish Dates only update in one direction. It seems that by default PublishOnly is set to True in the Field Mapping for MS Project for these fields, therefore if you have a schedule in Project and you publish it to the Team Foundation Server the work items will have the start and finish dates from project. However if you load up a project from the work items in your Team project the dates will not be reflected.

You can change this by downloading a mapping file, changing it and uploading it again. Use the command line utility TFSFieldMapping.exe to accompish this. Read this article to understand how to download and upload mapping information. 

The command is as simple as:
TFSFieldMapping download TeamFoundationServer TeamProject MappingFile.xml
TFSFieldMapping upload TeamFoundationServer TeamProject MappingFile.xml

After you have downloaded the Project Field Mapping information you can change the PublishOnly attribute to False and upload the file again.