简体   繁体   中英

Loading Assemblies from Resource C#

So I'm trying to keep all dependencies (including unmanaged - so no ILMerge) inside my portable application in C# .NET 2.0.

At first, I successfully wrote the DLLs to disk by using File.WriteAllBytes("DLLName.dll", Propertes.Resources.dll_name) so the program could read from these, but the problems were the time taken to write them and the clutter it caused.

Instead, I noticed Assembly.Load . This is the code I used to read the dependencies from my resources into the program:

try
{
    // Load OpenHardwareMonitorLib
    Assembly.Load(Properties.Resources.lib_hardware_monitor);

    // Some computers explicitly require 32 or 64 bit versions of SQLite. Load either version depending on architecture.
    if (Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "AMD64")
    {
        // 64 bit version
        Assembly.Load(Properties.Resources.lib_sqlite_x64);
    }
    else
    {
        // 32 bit version
        Assembly.Load(Properties.Resources.lib_sqlite_x86);
    }
}
catch ...

And that seems to work okay. Well, at least on my computer. It entered the catch block on my friend's PC, but I assume the answers you'll so kindly write will hit both birds with one stone.

However, this is the error I got from my computer after the try/catch when the program enters the main Application:

"Could not load file or assembly 'OpenHardwareMonitorLib, Version=0.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."

But of course, it's still looking for the actual DLL files. Now, how do I fix this?

Additional Details

OpenHardwareMonitorLib has no external dependencies. I have also updated the question to make clear the fact it's not working on my computer after the try/catch block, nor my friend's computer which fails in the shown try/catch block.

You are using the Assembly.Load(byte[]) overload. That's a pretty troublesome way to load assemblies in general. The specific problem you are having here is that you are trying to load a mixed-mode assembly. SqlLite contains both native and managed code. The overload you are using only supports pure managed assemblies.

You cannot do it this way.

I would recommend the simple and universal solution to your problem, one that also works on machines that have UAC to stop you from what you're trying to do. A single executable with the name "setup.exe".

Culprit is most likely one of the dependencies of the DLL rather than itself. So your code probably is OK but dependencies need to be installed on the machine.

You can use fuslogvw to see the errors.

May be one of the dependent dll missing on your friends computer. Please use Fusion Log Viewer to identify them

http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.71).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM