Getting OS Info via WMI

  • Thread starter Thread starter Primera
  • Start date Start date
P

Primera

Could someone please review my method below. I'm trying to return the type
of the OS on the local machine using the below. My concern is that there
could be a flaw in the if statement and the return statement with variable
scope on osName. I originally had the return statement inside the foreach
block, but I got an error 'not all code paths return a value'. After moving
the return statement outside of the foreach block I get no compile time errors.
I'm still a bit weak in the area of variable scope. Thanks in advance.



public static string osType()
{
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT
* FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection = query.Get();
string osName = null;

foreach (ManagementObject mo in queryCollection)
{
string os = Convert.ToString(mo.Properties["Name"].Value);
int testname = os.IndexOf("XP");

if (testname != -1)
{
osName = "WinXP";
}
else
{
osName = "Win2k";
}
Console.WriteLine(os);

}
return osName;

}
 
Hi,

Welcome to MSDN newsgroup!

Based on my understanding, if you want to get all results of the
¡°osName¡±, I suggest you use the array list to store these results.
However, if you just want to get the first result, please add the break
statement in the loop.

Some snippets of code like below for multi-result scenario:
public static ArrayList osType()
{
. . .
ArrayList osName = new ArrayList();
. .
if (testname != -1)
{
osName.Add("WinXP");
}
else
{
osName.Add("Win2k");
}
. . .
return osName;
}

Some snippets of code like below for first result scenario:
public static string osType()
{
. . .
foreach (ManagementObject mo in queryCollection)
{
. . .
break;
}
. . .
return osName;
}

I hope the above information helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
Primera said:
Could someone please review my method below. I'm trying to return the
type of the OS on the local machine using the below. My concern is that
there could be a flaw in the if statement and the return statement with
variable scope on osName. I originally had the return statement inside
the foreach block, but I got an error 'not all code paths return a value'.
After moving the return statement outside of the foreach block I get no
compile time errors. I'm still a bit weak in the area of variable scope.
Thanks in advance.



public static string osType()
{
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection queryCollection = query.Get();
string osName = null;

foreach (ManagementObject mo in queryCollection)
{
string os = Convert.ToString(mo.Properties["Name"].Value);
int testname = os.IndexOf("XP");

if (testname != -1)
{
osName = "WinXP";
}
else
{
osName = "Win2k";
}
Console.WriteLine(os); }
return osName;

}

Note that there is no need to use WMI for this. The framework class
System.Environment has all you are looking for.

string osName = "";
string osSPack = "";
OperatingSystem os = Environment.OSVersion;
switch (os.Version.Major)
{
case 5:
if (os.Version.Minor == 0)
osName = "Win2K";
if (os.Version.Minor == 1)
osName = "XP";
if (os.Version.Minor == 2)
osName = "Win2K3";
osSPack = os.ServicePack;
break;
case 6:
osName = "Vista";
break;
default:
break;
}

Willy.
 
Back
Top