473,769 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programmaticall y 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 2663
There's this......

using System.Manageme nt; // at a minimum

public int GetSPLevel ()
{
ObjectQuery oq = new ObjectQuery("se lect * from Win32_operating system WHERE
Primary=True");
ConnectionOptio ns opt = new ConnectionOptio ns ();
opt.Impersonati on = ImpersonationLe vel.Impersonate ;
opt.EnablePrivi leges = true;
string servername = ".";
ManagementScope scope = new ManagementScope ("\\\\" + servername +
"\\root\\cimv2" , opt);
scope.Connect() ;
ManagementObjec tSearcher sea = new ManagementObjec tSearcher(scope , oq);
StringBuilder st = new StringBuilder() ;
try
{
foreach (ManagementObje ct proc in sea.Get())
{
st.Append(proc["ServicePackMaj orVersion"]);
return Convert.ToInt32 (st.ToString()) ;
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return 0;
}

--
Phil Wilson [MVP Windows Installer]
----
"Ian Rowland" <Ia********@dis cussions.micros oft.com> wrote in message
news:15******** *************** ***********@mic rosoft.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.Manageme nt; // at a minimum

public int GetSPLevel ()
{
ObjectQuery oq = new ObjectQuery("se lect * from Win32_operating system WHERE
Primary=True");
ConnectionOptio ns opt = new ConnectionOptio ns ();
opt.Impersonati on = ImpersonationLe vel.Impersonate ;
opt.EnablePrivi leges = true;
string servername = ".";
ManagementScope scope = new ManagementScope ("\\\\" + servername +
"\\root\\cimv2" , opt);
scope.Connect() ;
ManagementObjec tSearcher sea = new ManagementObjec tSearcher(scope , oq);
StringBuilder st = new StringBuilder() ;
try
{
foreach (ManagementObje ct proc in sea.Get())
{
st.Append(proc["ServicePackMaj orVersion"]);
return Convert.ToInt32 (st.ToString()) ;
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return 0;
}

--
Phil Wilson [MVP Windows Installer]
----
"Ian Rowland" <Ia********@dis cussions.micros oft.com> wrote in message
news:15******** *************** ***********@mic rosoft.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 ConnectionOptio ns 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 GetWindowsVersi on ()
{
const string WINDOWS_VERSION _QUERY = "Select * From Win32_Operating System
WHERE Primary = True"; // Primary - specifies the primary OS
const string MANAGEMENT_PATH = @"\\.\root\cimv 2";
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();

ManagementObjec tSearcher managementObjec tSearcher = new
ManagementObjec tSearcher( managementScope , objectQuery );

ManagementObjec tCollection managementObjec tCollection =
managementObjec tSearcher.Get() ;

foreach ( ManagementObjec t proc in managementObjec tCollection )
{
if ( proc != null )
{
string caption = proc[ "Caption" ].ToString();
string servicePack = proc[ "CSDVersion " ].ToString();
string buildNumber = proc[ "BuildNumbe r" ].ToString();
version = string.Format( CultureInfo.Inv ariantCulture,
WINDOWS_VERSION _FORMAT, caption, servicePack, buildNumber );
break;
}
}
}
catch {}

return version;

}

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

using System.Manageme nt; // at a minimum

public int GetSPLevel ()
{
ObjectQuery oq = new ObjectQuery("se lect * from Win32_operating system WHERE
Primary=True");
ConnectionOptio ns opt = new ConnectionOptio ns ();
opt.Impersonati on = ImpersonationLe vel.Impersonate ;
opt.EnablePrivi leges = true;
string servername = ".";
ManagementScope scope = new ManagementScope ("\\\\" + servername +
"\\root\\cimv2" , opt);
scope.Connect() ;
ManagementObjec tSearcher sea = new ManagementObjec tSearcher(scope , oq);
StringBuilder st = new StringBuilder() ;
try
{
foreach (ManagementObje ct proc in sea.Get())
{
st.Append(proc["ServicePackMaj orVersion"]);
return Convert.ToInt32 (st.ToString()) ;
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return 0;
}

--
Phil Wilson [MVP Windows Installer]
----
"Ian Rowland" <Ia********@dis cussions.micros oft.com> wrote in message
news:15******** *************** ***********@mic rosoft.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
2456
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 Pack 1, English Version.) So I do the install Now whenever I check updates it still tells me that a 'critical update', the file in question "Microsoft .NET Framework Service Pack 2, English Version", still needs to be installed. After I install it...
2
1907
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 minute or two of execution. After many hours of frustration, I called Microsoft tech support and discussed the issue with them. After asking around, the support person said there indeed was a bug in the installation file, and to forget about it...
3
523
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 Win32API GetVersionEx!
18
2010
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 XP? we have our software on thousands of XP boxes and have not had a problem up to this point. Any Clues/suggestions on what it could be? Regards, Jay
2
1852
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 result. Is there any chance I could get the result by programming? Fill in data on "abc.aspx" --> Submit --> "def.aspx" requests the data --> Processing --> Returns the result to "abc.aspx"
4
2101
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 class, but I can't find a way to determine IE update version or service pack number. Any ideas? TIA Henry
1
1630
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 reinstalling updates except Service Pack 2. When I go to the download site it says I already have service pack 2 installed. There is an icon in my system tray saying that service pack 2 isn't installed. What can I do to get rid of the icon or...
33
11865
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...
13
6655
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 Framework Service Pack 1 in order to use ASP.net 1.1 and the net Framework 1.1 ------------------------------------------------------------------------------------------------ I found the following file, "svcpack.log" at C:\WINNT. It contains many...
3
2596
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 the forms input. I am aware that I can roll my own Create User form by using the membership API, but I am lazy and want to use most of the CreateUserWizard control's features. I tried converting the form to a template and setting the UserName...
0
9589
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
9423
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
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9999
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
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
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.