控件注册 - 利用资源文件将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
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Windows.Forms;  
  5.   
  6. using System.Diagnostics;  
  7.   
  8. namespace RegisterFile  
  9. {  
  10.     static class Program  
  11.     {  
  12.         /// <summary>  
  13.         /// 应用程序的主入口点。  
  14.         /// </summary>  
  15.         [STAThread]  
  16.         static void Main()  
  17.         {  
  18.             Application.EnableVisualStyles();  
  19.             Application.SetCompatibleTextRenderingDefault(false);  
  20.             Application.Run(new frmMain());  
  21.         }  
  22.     }  
  23.   
  24.   
  25.   
  26.     class Register  
  27.     {  
  28.         public void RegisterDll(string strDll)  
  29.         {  
  30.             Process p = new Process();  
  31.             p.StartInfo.FileName = "Regsvr32.exe";  
  32.   
  33.             p.StartInfo.Arguments = " " + strDll;  
  34.             p.Start();  
  35.   
  36.             p.Close();  
  37.         }  
  38.     }  
  39. }  

 

     

      最后,在Form1_Load()中添加代码:  

     

[c-sharp] view plaincopy
 
  1. //需要添加引用  
  2. //using System.IO;  
  3. //using System.Reflection;  
  4. //using System.Resources;          
  5.   
  6.   
  7.         private void Form1_Load(object sender, EventArgs e)  
  8.         {  
  9.             this.Visible = false;  
  10.   
  11.             string strPath = string.Empty;  
  12.             strPath = System.Environment.CurrentDirectory;  
  13.   
  14.   
  15.             Assembly asm = Assembly.GetEntryAssembly();  
  16.             using (Stream stream = asm.GetManifestResourceStream("RegisterFile.Resource.dsoframer.ocx"))  
  17.             {  
  18.                 int len = (int)stream.Length;  
  19.                 byte[] byts = new byte[len];  
  20.   
  21.                 stream.Read(byts, 0, len);  
  22.                 stream.Close();  
  23.   
  24.                 using (FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.System) + "//dsoframer.ocx", FileMode.Create))  
  25.                 {  
  26.                     fs.Write(byts, 0, len);  
  27.                 }  
  28.             }  
  29.   
  30.               
  31.   
  32.             Register r = new Register();  
  33.             r.RegisterDll("dsoframer.ocx");  
  34.   
  35.             this.Close();  
  36.         }  

 

 

 

     注意:Stream stream = asm.GetManifestResourceStream("RegisterFile.Resource.dsoframer.ocx")中"RegisterFile.Resource.dsoframer.ocx"的取值为“命名空间”+ “文件夹” + “文件名称”。

posted @ 2012-07-17 09:36  怡馨  阅读(1241)  评论(0编辑  收藏  举报