Categories
C#.NET Concept/algorithm Operating System

Refreshing USB ports using C#

Finished a project in which I need to refresh the USB ports by just before we start some particular benchmarking application my client has. Seems pretty easy job, if you know to gain access of system resource. But, it become fun job, when I found that knowledge of system is not even necessary for this job. Thanks to Microsoft’s `Devcon` utility, that already take care of it. So, in all what I have to do is just pass the correct parameter to it. The parameter is pretty easy, just pass the Hardware ID of USB port to refresh.

Yeah, that’s pretty easy. But …

Yes, but how we get the hardware id of USB port? Do we still need access to system resource for that? Well, it seems so. So we are back to square one. Well not really. I found a solution on web that points to `System.Management` namespace in .NET. What it does is it give you the list of all resources that are otherwise available through Computer management or such system resources. Here is main piece of code from article

public static List GetUSBDevices()
{
	List devices = new List();

	ManagementObjectCollection collection;
	//Win32_PnPEntity
	//Win32_USBControllerDevice
	using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
		collection = searcher.Get();

	foreach (var device in collection)
	{
		devices.Add(new USBDeviceInfo(
		(string)device.GetPropertyValue("DeviceID"),
		(string)device.GetPropertyValue("PNPDeviceID"),
		(string)device.GetPropertyValue("Description"),
		device.ToString()
		));
	}

	collection.Dispose();
	return devices;
}

This provide the list of all USB port in system, and then you can pass the “DeviceID” to devcon and we are done.

For further clarification:

Categories
.NET C#.NET

C#: Code to load Object from Database

It is often when managing old project that do not use ORM coding, we need to load object from database. In such scenarios it is quite boring to code all 30-40 property to load from DB. So, I wrote this simple code which does it quite well for me, the only catch is you need to define object property same as their DB column name. So, here is the quick code for the same.

public void LoadRS(DataRow rs)
    {
        foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
        {
            try
            {
                if (propertyInfo.CanRead)
                {
                    if (rs.Table.Columns.Contains(propertyInfo.Name))
                    {
                        
                        switch (propertyInfo.PropertyType.Name)
                        {
                            case "String":
                                propertyInfo.SetValue(this, Convert.ToString(rs[propertyInfo.Name]), null);
                                break;
                            case "Int64":
                                propertyInfo.SetValue(this, long.Parse(Convert.ToString(rs[propertyInfo.Name])), null);
                                break;
                            case "Int32":
                                propertyInfo.SetValue(this, int.Parse(Convert.ToString(rs[propertyInfo.Name])), null);
                                break;
                            default:
                                propertyInfo.SetValue(this, Convert.ChangeType(Convert.ToString(rs[propertyInfo.Name]), propertyInfo.PropertyType), null);
                                break;
                        }
                    }
                }
            }
            catch { }
        }
    }

  The code use the System.Reflection and then it read the Object property [see this.GetType().GetProperties() in foreach], iterate over properties and then checking if DataRow contain the same name column, if so, it load them. Basically the Default statement in Switch is enough for most cases, but I still leave it as is insight that in future I might need to cast some particular DB field to other field here. Oh, yes that one condition is boolean field. 

There are lot of upgradation coming in my mind as I write this blog entry, but currently no time to upgrade it and will add this function in my class library for future use.