Skrubb it Out

Thursday, November 16, 2006

Application "Who am I?"

For a while I've been thinking about trying to determine a few usefull pieces of information about a progam and the machine it is running on via code (C#).

EXE directory:
Because of the nature of the programs we write where I work, there is a VERY large amount of directory changing and file manipulation. One thing that has bit me in the butt, is not paying attention to what directory the program is using when it creates a file (specifically a config file that is written as the program terminates). Generally we can get away using absolute pathing, but in this case, we want the file to be placed directly in the directory containing the exe.

Unfortunately I rarely ever use logic and look in the right place for this information, but I've finally took the time to think about it and write it down:

string appPath = Application.StartupPath;
string fullPath = Application.ExecutablePath;
string appName = fullPath.Substring(appPath.Length + 1);
string appVersion = Application.ProductVersion;

MAC Address:
Another piece of information that I've been looking for is how to determine the mac and ip address of the machine a program is running on. I discovered that there are several network adapters on most machines but only one or two that are actually active. The following code (C#) grabs the mac & ip addressses for each ip active adapter:

//add System.Management to the project references first
query = new ManagementObjectSearcher("SELECT MacAddress,IPAddress FROM Win32_NetworkAdapterConfiguration where IPEnabled = TRUE");
queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection){
if (mo["MacAddress"] != null){
ipAdd = ((string[])mo["IPAddress"])[0];
macAdd = mo["MacAddress"].ToString();
break;
}
}

No fool proof and it only returns the first adapter, but good enough for what I need done. I also understand that this is much easier to do using vb script, but that's not in my bag of tricks.

0 Comments:

Post a Comment

<< Home