读书人

控件登记 - 利用资源文件将dll、ocx打

发布时间: 2012-12-20 09:53:21 作者: rapoo

控件注册 - 利用资源文件将dll、ocx打包进exe文件(C#版)

很多时候自定义或者引用控件都需要注册才能使用,但是如何使要注册的dll或ocx打包到exe中,使用户下载以后看到的只是一个exe,点击直接运行呢?就像很多安全控件,如支付宝的aliedit.exe那样。

????? 现在介绍一种使用资源文件,将dll、ocx打包进exe,点击直接注册的例子:

????? 首先,新建一个工程RegisterFile。? 新建文件夹Resource,里面添加需要注册的ocx或dll。这里我添加的是dsoframer.ocx,并将其文件“属性”中“生成操作”项的值改为“嵌入的资源”。

????? 控件登记 - 利用资源文件将dll、ocx打包进exe文件(C#版)

????? 接下来,创建类Register.cs?? 里面只有一个函数RegisterDll()。 这里为省事,我把它放到了Program.cs里,同一命名空间下,效果是一样的。????

????? view plaincopy to clipboardprint?
using System;??
using System.Collections.Generic;??
using System.Linq;??
using System.Windows.Forms;??
?
using System.Diagnostics;??
?
namespace RegisterFile??
{??
??? static class Program??
??? {??
??????? /// <summary>??
??????? /// 应用程序的主入口点。??
??????? /// </summary>??
??????? [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();??
??????? }??
??? }??
}?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using System.Diagnostics;

namespace RegisterFile
{
??? static class Program
??? {
??????? /// <summary>
??????? /// 应用程序的主入口点。
??????? /// </summary>
??????? [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()中添加代码:??

????? view plaincopy to clipboardprint?
//需要添加引用??
//using System.IO;??
//using System.Reflection;??
//using System.Resources;??????????
?
?
??????? 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();??
??????? }?
//需要添加引用
//using System.IO;
//using System.Reflection;
//using System.Resources;???????


??????? 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"的取值为“命名空间”+ “文件夹” + “文件名称”。

还有注册控件VB版。其实VB版才是先写的,后来才做的C#版。

?

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xrongzhen/archive/2010/08/05/5790313.aspx

读书人网 >C#

热点推荐