生活随笔
收集整理的這篇文章主要介紹了
控件注册 - 利用资源文件将dll、ocx打包进exe文件(转)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
很多時(shí)候自定義或者引用控件都需要注冊(cè)才能使用,但是如何使要注冊(cè)的dll或ocx打包到exe中,使用戶下載以后看到的只是一個(gè)exe,點(diǎn)擊直接運(yùn)行呢?就像很多安全控件,如支付寶的aliedit.exe那樣。
?
????? 現(xiàn)在介紹一種使用資源文件,將dll、ocx打包進(jìn)exe,點(diǎn)擊直接注冊(cè)的例子:
????? 首先,新建一個(gè)工程RegisterFile。? 新建文件夾Resource,里面添加需要注冊(cè)的ocx或dll。這里我添加的是dsoframer.ocx,并將其文件“屬性”中“生成操作”項(xiàng)的值改為“嵌入的資源”。
??????
????? 接下來(lái),創(chuàng)建類Register.cs?? 里面只有一個(gè)函數(shù)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"的取值為“命名空間”+ “文件夾” + “文件名稱”。
轉(zhuǎn)載于:https://www.cnblogs.com/xyqCreator/archive/2012/07/17/2594670.html
總結(jié)
以上是生活随笔為你收集整理的控件注册 - 利用资源文件将dll、ocx打包进exe文件(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。