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:

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

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
    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 = ""

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

    For Each xmlNodeS In xmlList
        aSupportedVersion &= "," & xmlNodeS.Attributes.GetNamedItem("version").Value()
    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)

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

    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 = 
    if (aRequiredVersion != "")
        Console.Writeline("Required Runtime Version: " + aRequiredVersion);

    if (aSupportedVersion != "") 
        Console.Writeline("Supported Runtime Version: " + aSupportedVersion );

About TheScarms
About TheScarms

Sample code
version info

If you use this code, please mention ""

Email this page

© Copyright 2024 TheScarms
Goto top of page