473,696 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
{
ManagementObjec tSearcher query = new ManagementObjec tSearcher("SELE CT
* FROM Win32_Operating System");
ManagementObjec tCollection queryCollection = query.Get();
string osName = null;

foreach (ManagementObje ct mo in queryCollection )
{
string os = Convert.ToStrin g(mo.Properties["Name"].Value);
int testname = os.IndexOf("XP" );

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

}
return osName;

}
Jan 6 '06 #1
2 2450
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("Win XP");
}
else
{
osName.Add("Win 2k");
}
. . .
return osName;
}

Some snippets of code like below for first result scenario:
public static string osType()
{
. . .
foreach (ManagementObje ct 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*****@newsgr oups.nospam> wrote in message
news:40******** *************** **@msnews.micro soft.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()
{
ManagementObjec tSearcher query = new
ManagementObjec tSearcher("SELE CT * FROM Win32_Operating System");
ManagementObjec tCollection queryCollection = query.Get();
string osName = null;

foreach (ManagementObje ct mo in queryCollection )
{
string os = Convert.ToStrin g(mo.Properties["Name"].Value);
int testname = os.IndexOf("XP" );

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

}


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

string osName = "";
string osSPack = "";
OperatingSystem os = Environment.OSV ersion;
switch (os.Version.Maj or)
{
case 5:
if (os.Version.Min or == 0)
osName = "Win2K";
if (os.Version.Min or == 1)
osName = "XP";
if (os.Version.Min or == 2)
osName = "Win2K3";
osSPack = os.ServicePack;
break;
case 6:
osName = "Vista";
break;
default:
break;
}

Willy.

Jan 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2582
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as I could not configure Apache myself. The REAL problem is that PHPmyAdmin works and sees my test database Wines.... But my PHP program does not!
7
6057
by: ivan_oj28 | last post by:
Hi, I am developing an application where I need to read the caller id from an incoming call. The only info (for now) I need is the caller id info, so that I can display the propper caller info on screen if so desired. I was thinking on using asterisk (asterisk.org) for this with appropriate 2FXO/2FXS card but I do not know if this is just overkill (asterisk for this). Do you know of any other solution/way of getting the caller id and...
4
9173
by: Rajesh Kumar Mallah | last post by:
Hi, We need to implement following logic efficiently. SELECT * from some_table where .... IF rows_matched = 1 THEN use the single row that matched.
1
2689
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I am missing something basic here... Need help... Thanks much
0
1177
by: Steve | last post by:
Hi All, I have a Python script that uses SOAPpy and I'm outputting all of the methods and info about the parameters... I'm having trouble getting information out of the __init__ parameter. My code : from SOAPpy import WSDL
4
1595
by: Max | last post by:
Hello, I live in Canada and I would like to get a copy of Visual Studio .NET 2005. I looked on Amazon and they don't ship to here and neither does Microsoft. I can't find any retailer that has VS .NET 2005. Am I looking in the wrong places or this product is just not available outside the States? Cheers, Max.
2
1734
by: sesling | last post by:
Our Database stores event messages in a table called eventnotifications. The table has a message type column. There are two types of messages (alert or info). I have created a form that includes a list box. The list box contains the following (alert, info, both). The ID for each value is hidden from the operator. alert has an ID of 1, info has an ID of 2 and both has an ID of 3. The list box value is passed to a query. The query is...
33
11849
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon (NT_AUTHORITY\SYSTEM). I understand that when the service starts, no user may be logged in, but that's ok, as the app I am monitoring can only be run by a logged in user. Do I need to use WMI to get the user context of Explorer.exe or is there a...
2
3572
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in uby/Rails. I started it in Perl and gave up on Perl as I went from the 'display the database information on the web page' to the 're-display the information from the database and allow the user to update the database using the web page' stage and realized...
0
2915
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form action="/login"> <html:text property="userName" value="Bunty"/> <html:submit/>
0
8666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9010
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.