Some articles in codeproject or other site help me on checking the version of .NET Framework installed on a machine. They use techniques to get information from various resources such as Registry or even look at the windows folder. You can see the articles on :
Code Project xFxDetection : http://www.codeproject.com/KB/dotnet/XFxDetect.aspx
The article above show about searching keys and values in specific location on registry. It can detect version including RTM and Service pack.
This is good for desktop application and not for web application. In web application, most of system administrator prevent access specific resource like registry. So I came with an idea of checking some features that is available in that machine with specific assembly.
First thing is about assembly that come with .NET Framework 2.0. We can easily choose any assembly that come from .NET Framework for example is mscorlib.dll. This is a basic assembly that must be existed. I just is it available by load that assembly into current application domain. Other assembly can be loaded with the same way.
In .NET Framework 3.0, There are 3 pillars of the Framework; There are Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), Windows Workflow Foundation (WF). Those all pillars come with new assembly. I choose PresentationCore as an assembly to load in my current application domain. That assembly belongs to WPF.
In .NET Framework 3.5, There are new functionality like LINQ and ASP.NET 3.5 Ajax. I choose System.Core since this assembly contain LINQ
In .NET Framework 3.5 with Service Pack 1, There is functionality like web routing. I choose System.Web.Abstraction to load because it belongs to web routing techniques.
The technique is really simple. I just load all that assembly with their culture, version and token; into my current application domain. If this process throw exception, the machine is not supporting that framework version.
The code below is a sample. I use an aspx without code behind so user can reuse and put on web server easily.
I give a name of this file : TestedFramework.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/C#" runat="server">
public const string Net20KnownAssembly = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
public const string Net30KnownAssembly = "PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
public const string Net35KnownAssembly = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
public const string Net35Sp1KnownAssembly = "System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35";
public const string ResponseFormat = ".Net Version {0} is {1}";
public enum ServerNetVersionInstalled
{
// not implemented
Net1x,
Net20,
Net30,
Net35,
Net35SP1,
}
public IEnumerable<ServerNetVersionInstalled> GetInstalledVersion()
{
List<ServerNetVersionInstalled> lstVersion = new List<ServerNetVersionInstalled>();
// try for .NET 2.0
try
{
AppDomain.CurrentDomain.Load(testedframework_aspx.Net20KnownAssembly);
lstVersion.Add(ServerNetVersionInstalled.Net20);
}
catch { }
// try for .NET 3.0
try
{
AppDomain.CurrentDomain.Load(testedframework_aspx.Net30KnownAssembly);
lstVersion.Add(ServerNetVersionInstalled.Net30);
}
catch { }
// try for .NET 3.5
try
{
AppDomain.CurrentDomain.Load(testedframework_aspx.Net35KnownAssembly);
lstVersion.Add(ServerNetVersionInstalled.Net35);
}
catch { }
// try for .NET 3.5 Sp1
try
{
AppDomain.CurrentDomain.Load(testedframework_aspx.Net35Sp1KnownAssembly);
lstVersion.Add(ServerNetVersionInstalled.Net35SP1);
}
catch { }
// try for .NET 1x
// not implemented
return lstVersion;
}
</script>
<style media="screen" type="text/css">
em {color:Red; font-weight:lighter; font-size:large;}
strong {color:Blue; font-weight:bolder; font-size:large;}
</style>
</head>
<body style="font-family:Arial;">
<form id="form1" runat="server">
<div style="height:100px; background-color:#66CCFF; vertical-align:middle; text-align:center;">
<h1>List of Framework Version Installed</h1>
</div>
<hr />
<div style="background-color:#CEE9F0;">
<%
List<ServerNetVersionInstalled> lstVersion = new List<ServerNetVersionInstalled>(this.GetInstalledVersion());
List<string> lstResponse = new List<string>();
string formated = "";
// .NET 2.0
if (lstVersion.IndexOf(ServerNetVersionInstalled.Net20) >= 0)
{
formated = string.Format(testedframework_aspx.ResponseFormat, "2.0", "<strong>Installed</strong>");
}
else
{
formated = string.Format(testedframework_aspx.ResponseFormat, "2.0", "<em>Not Installed</em>");
}
lstResponse.Add(formated);
// .NET 3.0
if (lstVersion.IndexOf(ServerNetVersionInstalled.Net30) >= 0)
{
formated = string.Format(testedframework_aspx.ResponseFormat, "3.0", "<strong>Installed</strong>");
}
else
{
formated = string.Format(testedframework_aspx.ResponseFormat, "3.0", "<em>Not Installed</em>");
}
lstResponse.Add(formated);
// .NET 3.5
if (lstVersion.IndexOf(ServerNetVersionInstalled.Net35) >= 0)
{
formated = string.Format(testedframework_aspx.ResponseFormat, "3.5", "<strong>Installed</strong>");
}
else
{
formated = string.Format(testedframework_aspx.ResponseFormat, "3.5", "<em>Not Installed</em>");
}
lstResponse.Add(formated);
// .NET 3.5 SP 1
if (lstVersion.IndexOf(ServerNetVersionInstalled.Net35SP1) >= 0)
{
formated = string.Format(testedframework_aspx.ResponseFormat, "3.5 SP 1", "<strong>Installed</strong>");
}
else
{
formated = string.Format(testedframework_aspx.ResponseFormat, "3.5 SP 1", "<em>Not Installed</em>");
}
lstResponse.Add(formated);
this.Response.Write("");
foreach (string item in lstResponse)
{
string html = string.Format("<p>{0}</p>",item);
this.Response.Write(html);
}
%>
</div>
</form>
</body>
</html>
2 comments:
Thanx
great idea, thanks for the information
Post a Comment