473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Programmatically Determining Service Pack For OS Using Managed Cod

Is there a way, using managed code, to determine the Service Pack of the OS
without resorting to registry calls?

OSVersion doesn't give this information. It seems the only way is to use the
Win32API GetVersionEx!
Nov 22 '05 #1
3 2636
There's this......

using System.Management; // at a minimum

public int GetSPLevel ()
{
ObjectQuery oq = new ObjectQuery("select * from Win32_operatingsystem WHERE
Primary=True");
ConnectionOptions opt = new ConnectionOptions ();
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
string servername = ".";
ManagementScope scope = new ManagementScope("\\\\" + servername +
"\\root\\cimv2", opt);
scope.Connect();
ManagementObjectSearcher sea = new ManagementObjectSearcher(scope, oq);
StringBuilder st = new StringBuilder();
try
{
foreach (ManagementObject proc in sea.Get())
{
st.Append(proc["ServicePackMajorVersion"]);
return Convert.ToInt32 (st.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return 0;
}

--
Phil Wilson [MVP Windows Installer]
----
"Ian Rowland" <Ia********@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Is there a way, using managed code, to determine the Service Pack of the
OS
without resorting to registry calls?

OSVersion doesn't give this information. It seems the only way is to use
the
Win32API GetVersionEx!

Nov 22 '05 #2
Phil,

Thanks for this - works great on W2K and XP.

Seem a little extreme to have to jump through these hoops just to get the
service pack number. It would have been much simpler to have OSVersion expose
the ServicePack property of the GetVersionEx Win32API.

Anyway, I prefer to use the managed code rather than calling unmanaged code
no matter how strange! Just hope I can get the code changes approved!

Regards,

Ian

"Phil Wilson" wrote:
There's this......

using System.Management; // at a minimum

public int GetSPLevel ()
{
ObjectQuery oq = new ObjectQuery("select * from Win32_operatingsystem WHERE
Primary=True");
ConnectionOptions opt = new ConnectionOptions ();
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
string servername = ".";
ManagementScope scope = new ManagementScope("\\\\" + servername +
"\\root\\cimv2", opt);
scope.Connect();
ManagementObjectSearcher sea = new ManagementObjectSearcher(scope, oq);
StringBuilder st = new StringBuilder();
try
{
foreach (ManagementObject proc in sea.Get())
{
st.Append(proc["ServicePackMajorVersion"]);
return Convert.ToInt32 (st.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return 0;
}

--
Phil Wilson [MVP Windows Installer]
----
"Ian Rowland" <Ia********@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Is there a way, using managed code, to determine the Service Pack of the
OS
without resorting to registry calls?

OSVersion doesn't give this information. It seems the only way is to use
the
Win32API GetVersionEx!


Nov 22 '05 #3
Phil,

I have modifed you code ( see below )so that I can get the full version of
the operating system including the Service Pack and Build Number. e.g.

"Microsoft Windows 2000 Professional Service Pack 4 (Build 2195)"

I have removed the ConnectionOptions security settings as the code appeared
to work without them on W2K and XP - should they be included. The
documentation for Win32_Operating_System WMI class
(http://msdn.microsoft.com/library/de...tingsystem.asp)
indicates that the security settings are really only required for Reboot,
SetDateTime, Shutdown and Win32Shutdown.

Regards

Ian

public static string GetWindowsVersion ()
{
const string WINDOWS_VERSION_QUERY = "Select * From Win32_OperatingSystem
WHERE Primary = True"; // Primary - specifies the primary OS
const string MANAGEMENT_PATH = @"\\.\root\cimv2";
const string WINDOWS_VERSION_FORMAT = "{0} {1} (Build {2})";

string version = string.Empty;

try
{
ObjectQuery objectQuery = new ObjectQuery( WINDOWS_VERSION_QUERY );

ManagementScope managementScope = new ManagementScope( MANAGEMENT_PATH );
managementScope.Connect();

ManagementObjectSearcher managementObjectSearcher = new
ManagementObjectSearcher( managementScope, objectQuery );

ManagementObjectCollection managementObjectCollection =
managementObjectSearcher.Get();

foreach ( ManagementObject proc in managementObjectCollection )
{
if ( proc != null )
{
string caption = proc[ "Caption" ].ToString();
string servicePack = proc[ "CSDVersion" ].ToString();
string buildNumber = proc[ "BuildNumber" ].ToString();
version = string.Format( CultureInfo.InvariantCulture,
WINDOWS_VERSION_FORMAT, caption, servicePack, buildNumber );
break;
}
}
}
catch {}

return version;

}

"Phil Wilson" wrote:
There's this......

using System.Management; // at a minimum

public int GetSPLevel ()
{
ObjectQuery oq = new ObjectQuery("select * from Win32_operatingsystem WHERE
Primary=True");
ConnectionOptions opt = new ConnectionOptions ();
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
string servername = ".";
ManagementScope scope = new ManagementScope("\\\\" + servername +
"\\root\\cimv2", opt);
scope.Connect();
ManagementObjectSearcher sea = new ManagementObjectSearcher(scope, oq);
StringBuilder st = new StringBuilder();
try
{
foreach (ManagementObject proc in sea.Get())
{
st.Append(proc["ServicePackMajorVersion"]);
return Convert.ToInt32 (st.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return 0;
}

--
Phil Wilson [MVP Windows Installer]
----
"Ian Rowland" <Ia********@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Is there a way, using managed code, to determine the Service Pack of the
OS
without resorting to registry calls?

OSVersion doesn't give this information. It seems the only way is to use
the
Win32API GetVersionEx!


Nov 22 '05 #4

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

Similar topics

4
by: RL | last post by:
On a Windows XP Pro system Using Windows Update I let it scan for updates - the only one it finds is "Microsoft .NET Framework Service Pack 2, English Version". (I have already installed Service...
2
by: Don Peters | last post by:
In spite of trying various ways of loading .NET Framework 1.1 Service Pack 1 under my new WinXP Service pack 1, I was unable to get it to install. Typically, the installation would hang after a...
3
by: Ian Rowland | last post by:
Is there a way, using managed code, to determine the Service Pack of the OS without resorting to registry calls? OSVersion doesn't give this information. It seems the only way is to use the...
18
by: jayderk | last post by:
a customer was trying to install some of our software after they installed the windows XP service pack 2 and he is getting a message that says something about it not being compatable with windows...
2
by: Jackal | last post by:
I have a form page(abc.aspx) that contains some fields need to be filled in before submitting, and the action of the form is "def.aspx". After peocessing the filled-in data, it'll return the...
4
by: henry | last post by:
Hi all, I'm building a website which provides contents that only work for some browser types and versions. I know I can get most of the client browser information using HttpBrowserCapabilities...
1
by: =?Utf-8?B?QmFyYg==?= | last post by:
I've been running with service pack 2 installed for a long time. Lately my system got unstable and I had to do a Windows XP Pro reinstall using the repair option. My system has automatically been...
33
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...
13
by: dancer | last post by:
I have made a new post because when I try to respond to another, I get the error, "Article Rejected -- Ill-formed message id" This is in response to the advice of Juan Libre to install Net...
3
by: Ken Fine | last post by:
I am using ASP.NET's CreateUserWizard control. I want to force the visitor to use a username and e-mail address that I am providing in programming, and I do not want the visitor to be able to edit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.