CTD with some (very few) .NET 2.0 XP SP2 Users

G

Guest

When I instatiate the following class like so:

public static HardDrive theHD = new HardDrive();

in the program.cs, the program crashes to desktop for some users. Most have
no reported problem.

This is the class:

class HardDrive
{
public HardDrive()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT
* FROM Win32_DiskDrive");

if (searcher != null)
{
foreach (ManagementObject wmi_HD in searcher.Get())
{
Model = wmi_HD["Model"].ToString();
Type = wmi_HD["InterfaceType"].ToString();
break;
}
}
else
{
// Error
MessageBox.Show("Exiting: searcher == null.");
Application.Exit();
}

searcher = new ManagementObjectSearcher("SELECT * FROM
Win32_PhysicalMedia");

if (searcher != null)
{
foreach (ManagementObject wmi_HD in searcher.Get())
{
if (wmi_HD["SerialNumber"] == null)
SerialNo = "None";
else
SerialNo = wmi_HD["SerialNumber"].ToString();
break;
}
}
else
{
// Error
MessageBox.Show("Exiting: searcher2 == null.");
Application.Exit();
}
}

public string Model
{
get { return model; }
set { model = value; }
}

public string Type
{
get { return type; }
set { type = value; }
}

public string SerialNo
{
get { return serialNo; }
set { serialNo = value; }
}

private string model = null;
private string type = null;
private string serialNo = null;
}

anyone know why this might crash some users?

Patrick
 
M

Michael Klingensmith

You may want to do two things. First check the values for null that you get from your ManagementObject. Ex:

string s = string.Empty;

if ( wmi_HD["Model"] != null )

s = wmi_HD["Model"].ToString()

2nd, consider some try...catch that will report the problem, but I think that by checking for null on the properties you'll have less problems already

Michael Klingensmith

http://www.seeknsnatch.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top