By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,896 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,896 IT Pros & Developers. It's quick & easy.

Programmatically Determining Service Pack For OS Using Managed Cod

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.