5

可能重复:
在 c# .exe 中嵌入 .net dll

我一直在尝试将 .dll(特别是 Ionic.Zip.dll)嵌入到我的应用程序中,然后使用 CodeDom 编译一个新的 .exe 并需要 Ionic.Zip.dll。我希望能够分发我的程序而无需任何额外的 .dll。如果工作目录中有DLL,则程序和编译后的程序运行良好。我没有使用 ILMerge,因为编译后的程序需要 .dll,我不能强迫用户获取 ILMerge。但是,如果没有 .dll,我会收到此错误。

原始程序不会编译出现此错误的代码

error CS0006: Metadeta file 'Ionic.Zip.dll" could not be found.

编译后的程序(在原程序工作目录下有dll时编译)

Description:
Stopped working

Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01:   scrub.exe
Problem Signature 02:   0.0.0.0
Problem Signature 03:   50b0f364
Problem Signature 04:   Scrub
Problem Signature 05:   0.0.0.0
Problem Signature 06:   50b0f364
Problem Signature 07:   1
Problem Signature 08:   38
Problem Signature 09:   System.IO.FileNotFoundException
OS Version: 6.1.7601.2.1.0.256.48
Locale ID:  1033

调试错误

Could not load file or assembly 'Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c' or one of its dependencies. The system cannot find the file specified.

在 CodeDom 编译器中,我使用了以下代码:

Params.ReferencedAssemblies.Add("Ionic.Zip.dll");

为了解决这个问题,我已经尝试过并尽可能地关注它。http://adamthetech.com/2011/06/embed-dll-files-within-an-exe-c-sharp-winforms/

这是我在原始应用程序中的代码:

        public MainWindow()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            string resourceName = new AssemblyName(args.Name).Name + ".dll";
            string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));

            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
            {
                Byte[] assemblyData = new Byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
        };

        InitializeComponent();

        inniZip();
     }

    private void inniZip()
    {
        Ionic.Zip.ZipOption test = new Ionic.Zip.ZipOption();
    }

我也在编译的程序源中放入了类似的代码。我已经被这个错误困住了一段时间,我似乎无法弄清楚。

提前致谢,

4

0 回答 0