Search Amazon:

Read a .NET .Config file as an XML document to get the application's Runtime Version

Your .NET Application Configuration File specifies what version(s) of the CLR (Common Language Runtime) your application supports. The config file is an XML file having the same name as your executable with an additional .config extension. As the name implies, this file stores configuration information used by your application including the version of the required .NET Framework.

Information stored consists of information stored by the VS.NET IDE such as CLR version and dependent assembly information. You can also store your own application settings there as well. To see how to retrieve custom appSettings data click here.

.NET applications can target multiple runtime versions as specified by the supportedRuntime elements in the configuration file. A sample .config file might look like:

    <configuration> 
      <startup>
        <supportedRuntime version="v1.1.4322"/>
        <requiredRuntime version="v1.0.3705"/>
      </startup>
    </configuration>

When multiple CLR versions are supported the preferred version should be listed first and the least preferred listed last. The supportedRuntime element should be used by all applications using version 1.1 or later of the .NET Framework. If no version is specified, the version of the runtime used to build your application is used.

.NET applications built to support version 1.0 of the framework should use the requiredRuntime element instead.

Since the configuration file is an XML file you can retrieve this information in VB.NET easily:

    '
    ' Verify the configuration file exists.
    '
    Dim aConfigPath As String = Application.ExecutablePath() & ".config"

    If Not System.IO.File.Exists(aConfigPath) Then
        Application.Exit()
    End If

    '
    ' Read the runtime versions.
    '
    Dim xmlDoc   As New Xml.XmlDocument
    Dim xmlNode  As Xml.XmlNode
    Dim xmlNodeR As Xml.XmlNode
    Dim xmlNodeS As Xml.XmlNode
    Dim xmlList As Xml.XmlNodeList
    Dim aRequiredVersion  As String = ""
    Dim aSupportedVersion As String = ""

    xmlDoc.Load(aConfigPath)
    xmlNode = xmlDoc.DocumentElement.SelectSingleNode("startup")   
    xmlList = xmlNode.SelectNodes("supportedRuntime")

    For Each xmlNodeS In xmlList
        aSupportedVersion &= "," & xmlNodeS.Attributes.GetNamedItem("version").Value()
    Next
    aSupportedVersion = aSupportedVersion.Substring(1)

    xmlNodeR = xmlNode.SelectSingleNode("requiredRuntime")
    aRequiredVersion = xmlNodeR.Attributes.GetNamedItem("version").Value()

    If aRequiredVersion <> "" Then
        Debug.Writeline("Required Runtime Version: " & aRequiredVersion)
    End If

    If aSupportedVersion <> "" Then
        Debug.Writeline("Supported Runtime Version: " & aSupportedVersion)
    End If

The C# code looks like:

    //
    // Verify the configuration file exists.
    //
    string aConfigPath = Application.ExecutablePath + ".config";

    if (System.IO.File.Exists(aConfigPath) == false)
    {
    Application.ExitThread();
    }

    //
    // Read the runtime versions.
    //
    XmlNode xmlNode;
    XmlNodeList xmlList;
    XmlDocument xmlDoc = new XmlDocument();
    string aRequiredVersion = "";
    string aSupportedVersion  = "";

    xmlDoc.Load(aConfigPath);
    xmlNode = xmlDoc.DocumentElement.SelectSingleNode("startup");
    xmlList = xmlNode.SelectNodes("supportedRuntime");

    foreach (XmlNode xmlNodeS in xmlList)
    {
        aSupportedVersion += "," + xmlNodeS.Attributes.GetNamedItem("version").Value;
    }
    aSupportedVersion = aSupportedVersion.Substring(1);

    aRequiredVersion = 
      xmlNode.SelectSingleNode("requiredRuntime").Attributes.GetNamedItem("version").Value;
    if (aRequiredVersion != "")
    {
        Console.Writeline("Required Runtime Version: " + aRequiredVersion);
    }

    if (aSupportedVersion != "") 
    {
        Console.Writeline("Supported Runtime Version: " + aSupportedVersion );
    }
Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms