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!