Archive for the ‘Code’ Category

Network Link Detection in C#

August 3, 2007

This code snippit will show you how to detect if there is a network link on one of the network adaptors on your system. The function is written in C# and uses the Windows Management Instrumentation to get the link status.

/// <summary>
/// Checks if a network is connected to the local machine.
/// </summary>
/// <returns>true if network connected, false if not</returns>
static public boolIsNetworkConnected()
{
    bool connected=false;
    if(SystemInformation.Network)
    {
        System.Management.ManagementObjectSearcher searcher=
                      new System.Management.ManagementObjectSearcher(
                      "SELECT NetConnectionStatus FROM Win32_NetworkAdapter");  

        foreach(System.Management.ManagementObject networkAdapter in searcher.Get())
        {
            if(networkAdapter["NetConnectionStatus"]!=null)
            {
                if((int)networkAdapter["NetConnectionStatus"]==2)
                {
                    connected = true;
                    break;
                }
            }
        }
        searcher.Dispose();
    }
    return connected;
}

Fixing “The application failed to initialize properly (0xc0000135)”

August 1, 2007

When attempting to run Microsoft .Net based applications, like many of those available from http://www.lowesoftware.com, you may get the following error on startup:

The application failed to initialize properly (0xc0000135)

The cause of this error is that the Microsoft .Net framework is not installed or is damaged in some way. Therefore applications will fail to launch.

To fix this error you can run Windows Update and find the Microsoft .Net framework update to install. Alternatively, you can download the Microsoft .Net Framework directly from Microsoft.


Network Link Detection in C#

July 23, 2007

This code snippit will show you how to detect if there is a network link on one of the network adaptors on your system. The function is written in C# and uses the Windows Management Instrumentation to get the link status.

It is ready to use, as is. It has been used on many projects, personal and professional.

/// <summary>
/// Checks if a network is connected to the local machine.
/// </summary>
/// <returns>true if network connected, false if not</returns>   

static public bool IsNetworkConnected()
{
    bool connected=false;     if(SystemInformation.Network)   

    {
        System.Management.ManagementObjectSearcher searcher=new System.Management.ManagementObjectSearcher
                            ("SELECT NetConnectionStatus FROM Win32_NetworkAdapter");
        foreach(System.Management.ManagementObject networkAdapter in searcher.Get())
        {
            if(networkAdapter["NetConnectionStatus"]!=null)
            {
                if((int)networkAdapter["NetConnectionStatus"]==2)
                {
                    connected = true;
                    break;
                }
            }
        }
        searcher.Dispose();
    }
    return connected; }

Detecting Installed Microsoft .Net Framework (CLR) Version Information

July 20, 2007

It can be difficult to detect which versions of the Microsoft .Net framework are installed and available on a Windows device. This article presents a method to detect information about installed Microsoft .Net Framework versions.

In addition to describing the method this article provides a functional class to query and detect the installed CLR versions as well as a command line tool that will print out .Net CLR information.

Method

There is no simple function call in the .Net framework to detect versions of installed frameworks. This does make sense though, because in the case that .Net is not available it would be impossible to execute such a function call. C++ native code solutions do exist to perform this function and one such implementation can be found on Aaron Stebner’s MSDN Blog here.

The method used in the tool provided in this article is to enumerate the installed frameworks in the %systemroot%\Microsoft.NET\Framework folder, open the mscorlib.dll files, and retrieve version build information. These version numbers can then be looked up in a table of descriptions to provide information about the service pack level the .Net version is.

Command Line Tool

A command line tool to query the installed Microsoft .Net frameworks can be downloaded here. This Lowe*Software tool was written using the C# snippet below.

Note that this tool is compiled under Microsoft .Net Framework 1.1 and requires a version of .Net to run.

C# Code

using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Text.RegularExpressions; 

namespace DotNetVersionInfo
{
    /// <summary>
    /// This class is used to retrieve information about the 
    /// Microsoft .Net Frameworks installed.
    /// </summary>
    public sealed class DotNetVersion
    {
        private DotNetVersion()
        {
        } 

        /// <summary>
        /// Retrieves the version of the CLR version the process is
        /// currently executing in.
        /// </summary>
        /// <returns>A string representing the CLR version.</returns>
        public static string GetCurrentCLRVersion()
        {
            return System.Environment.Version.ToString();
        } 

        /// <summary>
        /// Retrieves a description of a CLR based on version. This
        /// method will return information about service pack levels.
        /// </summary>
        /// <param name="version">A string representing the CLR version.</param>
        /// <returns>A description of the CLR.</returns>
        public static string GetCLRVersionDescription(string version)
        {
            switch(version)
            {
                case "1.0.3705.0":
                    return ".NET Framework 1.0 Original Release";
                case "1.0.3705.209":
                    return ".NET Framework 1.0 Service Pack 1";
                case "1.0.3705.288":
                    return ".NET Framework 1.0 Service Pack 2";
                case "1.0.3705.6018":
                    return ".NET Framework 1.0 Service Pack 3";
                case "1.1.4322.573":
                    return ".NET Framework 1.1 Original Release";
                case "1.1.4322.2032":
                    return ".NET Framework 1.1 Service Pack 1";
                case "1.1.4322.2300":
                    return ".NET Framework 1.1 Service Pack 1 (Windows Server 2003 SP1)";
                case "2.0.40607.16":
                    return ".NET Framework 2.0 Beta 1";
                case "2.0.50215.44":
                    return ".NET Framework 2.0 Beta 2";
                case "2.0.50727.42":
                    return ".NET Framework 2.0 Original Release";
                default:
                    return "Unknown Version";
            }
        } 

        /// <summary>
        /// Retrieves a list of versions installed on the current machine.
        /// </summary>
        /// <returns>A string array of installed CLR versions.</returns>
        public static string[] GetInstalledCLRVersions()
        {
            ArrayList versions=new ArrayList(); 

            string frameworkpath=Path.Combine(
                Environment.SystemDirectory,
                ;@"..Microsoft.NETFramework"); 

            string[] dirs=Directory.GetDirectories(frameworkpath, "v*.*.*"); 

            foreach(string dir in dirs)
            {
                try
                {
                    if(Regex.IsMatch(dir, "v[0-9]{1,5}.[0-9]{1,5}.[0-9]{1,5}.[0-9]{1,5}"))
                    {
                        FileVersionInfo verinfo=FileVersionInfo.GetVersionInfo(
                            Path.Combine(frameworkpath, dir + ;@"Mscorlib.dll")); 

                        versions.Add(
                            verinfo.FileMajorPart + "." +
                            verinfo.FileMinorPart + "." +
                            verinfo.FileBuildPart + "." +
                            verinfo.FilePrivatePart);
                    }
                }
                catch {}
            } 

            return (string[])versions.ToArray(typeof(string));
        }
    }
}

References

http://support.microsoft.com/default.aspx?kbid=318785

http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx

http://blogs.msdn.com/astebner/archive/2004/09/18/231253.aspx