Programmatically Determining Service Pack For OS Using Managed Cod

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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,

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 said:
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 said:
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!
 
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/d...ry/en-us/wmisdk/wmi/win32_operatingsystem.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 said:
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 said:
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!
 
Back
Top