0

I'm using a third party API to access some data acquisition hardare (National Instruments hardware with DAQmx driver). To do so, I add a reference to its driver dll.

When I run the code on a machine that has the driver installed, no problem. But when I run on a machine without the driver, I get a System.IO.FileNotFoundException that cannot be caught in a try/catch.

How can I check, before I execute API code, if the dll is available and its types can be used. This is important because not all machines will support this kind of data acquisition hardware (and thus have the driver installed).

I'm not sure, but I think the dll is registered in the GAC on the machines that have the driver installed.

4

1 回答 1

0

You'll want to do something like the following. Just change it to return a true/false if the assembly exists. (taken from here)

static void Main(string[] args)
    {
        AssemblyLoader("System.Xml, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
        AssemblyLoader("System.Xml", false);
        AssemblyLoader("System.Drawing", true);
    }

    public static void AssemblyLoader(string LoadedAssemblyName, bool PartialName)
    {
        try
        {
            Assembly LoadedAssembly;
            Console.WriteLine("| Loading Assembly {0}", LoadedAssemblyName);
            if(PartialName == true)
                LoadedAssembly = Assembly.LoadWithPartialName(LoadedAssemblyName);
            else
                LoadedAssembly = Assembly.Load(LoadedAssemblyName);

            Console.WriteLine("Full Name: {0}", LoadedAssembly.FullName);
            Console.WriteLine("Location: {0}", LoadedAssembly.Location);
            Console.WriteLine("Code Base: {0}", LoadedAssembly.CodeBase);
            Console.WriteLine("Escaped Code Base: {0}", LoadedAssembly.EscapedCodeBase);
            Console.WriteLine("Loaded from GAC: {0}", LoadedAssembly.GlobalAssemblyCache);
        } catch(FileNotFoundException) {
            Console.WriteLine("EXCEPTION: Cannot load assembly.");
        }
    }
于 2010-02-23T18:10:09.097 回答