将一个库嵌入到我的应用程序中,并使其使用该库[重复]

问题描述 投票:1回答:3

我希望可执行文件成为程序正常工作所需的唯一内容,但它取决于库,尤其是MySql.Data.dll。我如何将这个库合并到我的可执行文件中?

我已经将DLL拖到了项目浏览器中。此外,我之后选择了它,并将其Build Action设置为Embedded Resource

但是当我在应用程序中尝试打开一个新窗口时,我仍然遇到FileNotFound异常:

在PresentationFramework.dll中发生了'System.IO.FileNotFoundException类型的未处理的异常

其他信息:无法加载文件或程序集'MySql.Data,版本= 6.9.8.0,区域性=中性,PublicKeyToken = c5687fc88969c44d'或其依赖项之一。系统找不到指定的文件。

编辑:

这与使用WinForms时有所不同吗?我之前已经使用WinForms应用程序进行了此操作,在该应用程序中我使用了ioniczip和一个json库,并将其作为嵌入式资源包括在内。这样,我就不需要在应用程序中包含两个DLL文件。

c# mysql embedded-resource
3个回答
0
投票

通常,您需要将所有依赖项复制到PATH环境变量中指定的文件夹中,或将它们放在可执行文件的同一文件夹中。您可能会发现这篇文章很有帮助Search Path Used by Windows to Locate a DLL

如果要捆绑dll,则可以签出“ ILMerge”。

关于“ System.IO.FileNotFoundException”或“ System.IO.BadImageException”之类的一般错误,您可以尝试使用“ Dependency Walker”来确定缺少哪些dll。


0
投票

您不能使用嵌入式资源来分发库。您需要像ILMerge这样的东西。参见http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx


0
投票

我找到了另一种解决方案。将DLL文件添加为项目的资源,然后将其添加到App.xaml.cs

public App()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}

System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string dllName = args.Name.Contains(',') ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
    dllName = dllName.Replace(".", "_");
    if (dllName.EndsWith("_resources")) 
        return null;
    System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
    byte[] bytes = (byte[])rm.GetObject(dllName);
    return System.Reflection.Assembly.Load(bytes);
}

这似乎对我有用。我在一个较旧的项目中找到了它,而我需要同样的东西(只是忘了它)

© www.soinside.com 2019 - 2024. All rights reserved.