471,618 Members | 1,195 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

Getting OS Info via WMI

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;

}
Jan 6 '06 #1
2 2343
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

Jan 6 '06 #2

"Primera" <Pr*****@newsgroups.nospam> wrote in message
news:40*************************@msnews.microsoft. com...
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.

Jan 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by ivan_oj28 | last post: by
4 posts views Thread by Rajesh Kumar Mallah | last post: by
reply views Thread by Steve | last post: by
33 posts views Thread by JamesB | last post: by
reply views Thread by buntyindia | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.