生活随笔
收集整理的這篇文章主要介紹了
控件注册 - 利用资源文件将dll、ocx打包进exe文件(转)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很多時候自定義或者引用控件都需要注冊才能使用,但是如何使要注冊的dll或ocx打包到exe中,使用戶下載以后看到的只是一個exe,點擊直接運行呢?就像很多安全控件,如支付寶的aliedit.exe那樣。
?
????? 現在介紹一種使用資源文件,將dll、ocx打包進exe,點擊直接注冊的例子:
????? 首先,新建一個工程RegisterFile。? 新建文件夾Resource,里面添加需要注冊的ocx或dll。這里我添加的是dsoframer.ocx,并將其文件“屬性”中“生成操作”項的值改為“嵌入的資源”。
??????
????? 接下來,創建類Register.cs?? 里面只有一個函數RegisterDll()。?這里為省事,我把它放到了Program.cs里,同一命名空間下,效果是一樣的。????
?????
[c-sharp]?view plaincopy
using?System;??using?System.Collections.Generic;??using?System.Linq;??using?System.Windows.Forms;????using?System.Diagnostics;????namespace?RegisterFile??{??????static?class?Program??????{????????????????????????????????????????[STAThread]??????????static?void?Main()??????????{??????????????Application.EnableVisualStyles();??????????????Application.SetCompatibleTextRenderingDefault(false);??????????????Application.Run(new?frmMain());??????????}??????}????????????class?Register??????{??????????public?void?RegisterDll(string?strDll)??????????{??????????????Process?p?=?new?Process();??????????????p.StartInfo.FileName?=?"Regsvr32.exe";????????????????p.StartInfo.Arguments?=?"?"?+?strDll;??????????????p.Start();????????????????p.Close();??????????}??????}??}?? ?
?????
????? 最后,在Form1_Load()中添加代碼:??
?????
[c-sharp]?view plaincopy
????????????????????private?void?Form1_Load(object?sender,?EventArgs?e)??????????{??????????????this.Visible?=?false;????????????????string?strPath?=?string.Empty;??????????????strPath?=?System.Environment.CurrentDirectory;??????????????????Assembly?asm?=?Assembly.GetEntryAssembly();??????????????using?(Stream?stream?=?asm.GetManifestResourceStream("RegisterFile.Resource.dsoframer.ocx"))??????????????{??????????????????int?len?=?(int)stream.Length;??????????????????byte[]?byts?=?new?byte[len];????????????????????stream.Read(byts,?0,?len);??????????????????stream.Close();????????????????????using?(FileStream?fs?=?new?FileStream(Environment.GetFolderPath(Environment.SpecialFolder.System)?+?"//dsoframer.ocx",?FileMode.Create))??????????????????{??????????????????????fs.Write(byts,?0,?len);??????????????????}??????????????}????????????????????????????????Register?r?=?new?Register();??????????????r.RegisterDll("dsoframer.ocx");????????????????this.Close();??????????}?? ?
?
?
?????注意:Stream stream = asm.GetManifestResourceStream("RegisterFile.Resource.dsoframer.ocx")中"RegisterFile.Resource.dsoframer.ocx"的取值為“命名空間”+ “文件夾” + “文件名稱”。
轉載于:https://www.cnblogs.com/xyqCreator/archive/2012/07/17/2594670.html
總結
以上是生活随笔為你收集整理的控件注册 - 利用资源文件将dll、ocx打包进exe文件(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。