把DLL檔案打包進EXE的技巧(C#)

來源:互聯網
上載者:User

(原文)http://www.cnblogs.com/zengxlf/archive/2009/05/17/1458996.html

廢話就不多說了,直接進入主題吧

用VS2005建立一個windows項目,取名test

引用dll檔案

編寫代碼,正常引用dll裡的類庫,

同時在test項目添加資源檔(該檔案就是剛才引用的dll檔案)

VS2005會自動產生引用代碼,我這裡引用的是IrisSkin2.dll

internal static byte[] IrisSkin2 {
            get {
                object obj = ResourceManager.GetObject("IrisSkin2", resourceCulture);
                return ((byte[])(obj));
            }
        }

然後在Main(program.cs)函數裡加入代碼

 

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string path = Application.StartupPath + "\";
            string dllFileName = "IrisSkin2.dll";
            //******載入IrisSkin2.dll******
            if (!File.Exists(path + dllFileName))   //檔案不存在
            {
                FileStream fs = new FileStream(path + dllFileName, FileMode.CreateNew, FileAccess.Write);
                byte[] buffer = GetData.Properties.Resources.IrisSkin2;//{GetData是命名空間}
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
            //*****************************
            
            Application.Run(new GDForm());
        }

編譯test項目,產生exe檔案,

然後刪除引用的dll檔案(注意是先編譯,再刪除)

複製該exe檔案就可以在別的地方運行了(不用dll,運行EXE會自動產生DLL檔案)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.