Scott Hanselman

Programmatically determine what version of ASP.NET you're running

October 15, '04 Comments [1] Posted in ASP.NET
Sponsored By

Here's a goodness from Doug Seven and various KBs. Thanks! 

<html><body><h1><asp:Label ID="Output" RunAt="server" /></h1></body></html>

<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e) {

//Microsoft .NET Framework 1.0 (no service pack) 1.0.3705.000
//Microsoft .NET Framework 1.0 Service Pack 1 (SP1) 1.0.3705.209
//Microsoft .NET Framework 1.0 Service Pack 2 (SP2) 1.0.3705.288
//Microsoft .NET Framework 1.0 Service Pack 3 (SP3) 1.0.3705.6018

string servicePack = "";
if(Environment.Version.Build==3705)
{
  switch(Environment.Version.Revision)
  {
  case 209:
  servicePack = "SP1";
  break;
  case 288:
  servicePack = "SP1";
  break;
  case 6018:
  servicePack = "SP1";
  break;
  }
}
else if(Environment.Version.Build==4322)
{
  using(
  Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\NETFramework Setup\NDP\v1.1.4322"))
  {
    if(regKey.GetValue("SP").ToString() =="1")
       servicePack = "SP1";
  }
}

//HKEY_LOCAL_MACHINE\Software\Microsoft\NET FrameworkSetup\NDP\v1.1.4322
Output.Text = String.Format ("This page uses ASP.NET {0}.{1} {2}", Environment.Version.Major, Environment.Version.Minor, servicePack);
}
</script>

Now playing: Alicia Keys - If I Was Your Woman/Walk On By

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Saturday, October 16, 2004 11:28:59 AM UTC
Hi,

A couple of typos in the servicePack assignment - suggested code below...

string servicePack = "";
if(Environment.Version.Build==3705)
{
switch(Environment.Version.Revision)
{
case 209:
servicePack = "SP1";
break;
case 288:
servicePack = "SP2"; // Fix typo
break;
case 6018:
servicePack = "SP3"; // Fix typo
break;
}
}
Martin Naughton
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.