473,785 Members | 2,460 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 2665
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
2458
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
2013
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
2102
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
9645
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
9480
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
10147
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...
0
8972
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
6739
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.