将.dll嵌入到.exe中

时间:2012-11-24 17:40:27

标签: c# dll embed dotnetzip

  

可能重复:
  Embed .net dll in c# .exe

我一直在尝试将.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();
    }

我也在编译的程序源中添加了类似的代码。 我一直坚持这个错误一段时间,我似乎无法弄明白。

提前致谢,

0 个答案:

没有答案