简体   繁体   English

从资源C#加载程序集

[英]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. 因此,我尝试将所有依赖项(包括非托管的-包括非ILMerge)都保留在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. 首先,我通过使用File.WriteAllBytes("DLLName.dll", Propertes.Resources.dll_name)成功地将DLL写入磁盘,因此程序可以从中读取这些DLL,但问题是写入它们所花费的时间以及使它们混乱造成的。

Instead, I noticed Assembly.Load . 相反,我注意到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. 它进入了我朋友PC上的catch块,但我想您会这么好地写出答案,将用一块石头击中两只鸟。

However, this is the error I got from my computer after the try/catch when the program enters the main Application: 但是,这是在程序进入主应用程序后,在try / catch后从计算机上收到的错误:

"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." “无法加载文件或程序集'OpenHardwareMonitorLib,版本= 0.4.0.0,文化=中性,PublicKeyToken =空”或其依赖项之一。系统找不到指定的文件。”

But of course, it's still looking for the actual DLL files. 但是,当然,它仍在寻找实际的DLL文件。 Now, how do I fix this? 现在,我该如何解决?

Additional Details 额外细节

OpenHardwareMonitorLib has no external dependencies. OpenHardwareMonitorLib没有外部依赖项。 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. 我还更新了问题,以弄清楚在try / catch块之后它在我的计算机上不起作用,或者所示的try / catch块中我朋友的计算机出现故障的事实。

You are using the Assembly.Load(byte[]) overload. 您正在使用Assembly.Load(byte [])重载。 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. SqlLite包含本机代码和托管代码。 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. 我将为您的问题推荐一种简单且通用的解决方案,该解决方案也可以在具有UAC的计算机上使用,以阻止您尝试执行的操作。 A single executable with the name "setup.exe". 名为“ setup.exe”的单个可执行文件。

Culprit is most likely one of the dependencies of the DLL rather than itself. 罪魁祸首很可能是DLL而非其本身的依赖项之一。 So your code probably is OK but dependencies need to be installed on the machine. 因此,您的代码可能没问题,但是需要在计算机上安装依赖项。

You can use fuslogvw to see the errors. 您可以使用fuslogvw查看错误。

May be one of the dependent dll missing on your friends computer. 可能是您的朋友计算机上缺少的依赖dll之一。 Please use Fusion Log Viewer to identify them 请使用Fusion Log Viewer进行识别

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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