C# dsoframer.ocx 控件注册

65 篇文章 0 订阅
22 篇文章 0 订阅

转自:http://blog.csdn.net/shuaishifu/article/details/38412643


  C# 应用程序中嵌入了 Word 窗体,运行程序调用 Word 窗体的时候报错。参照 错误详细说明

    闲着无事就编写了一个控制台程序,实现 dsoframer.ocx 控件 的注册功能。供参考。

  

代码如下:

   

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using Microsoft.Win32;  
  7. using System.Management;  
  8.   
  9. namespace dsoframerRegister  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             string fileFullPath = string.Format("{0}\\dsoframer.ocx", System.Environment.CurrentDirectory);  
  16.             string systemDrive = System.Environment.GetEnvironmentVariable("systemdrive");//获取系统所在的盘符  
  17.             if (!File.Exists(fileFullPath) || String.IsNullOrEmpty(systemDrive))  
  18.                 return;  
  19.   
  20.             bool isRegisted = IsRegistered("00460182-9E5E-11D5-B7C8-B8269041DD57");  
  21.             if (isRegisted)  
  22.                 return;  
  23.   
  24.   
  25.             string windowsPath = string.Empty;  
  26.   
  27.             if (GetOSBitCount() == "64")  
  28.                 windowsPath = string.Format("{0}\\Windows\\SysWOW64", systemDrive);  
  29.             else  
  30.                 windowsPath = string.Format("{0}\\Windows\\System32", systemDrive);  
  31.   
  32.             if (!Directory.Exists(windowsPath))  
  33.                 return;  
  34.   
  35.   
  36.             File.Copy(fileFullPath, string.Format("{0}\\dsoframer.ocx", windowsPath), true);  
  37.   
  38.             Registe(string.Format("{0}\\dsoframer.ocx", windowsPath));  
  39.   
  40.         }  
  41.   
  42.         //注册dsoframer.ocx  
  43.         private static bool Registe(string fileFullName)  
  44.         {  
  45.             bool result = false;  
  46.             System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");//注册完毕不显示是否成功的提示  
  47.             //System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName);//注册完毕显示是否成功的提示  
  48.             if (p != null && p.HasExited)  
  49.             {  
  50.                 Int32 exitCode = p.ExitCode;  
  51.                 if (exitCode == 0)  
  52.                     result = true;  
  53.             }  
  54.             return result;  
  55.         }  
  56.   
  57.         //获取当前操作系统的位数  
  58.         private static string GetOSBitCount()  
  59.         {  
  60.             ConnectionOptions oConn = new ConnectionOptions();  
  61.             System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost", oConn);  
  62.             System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select AddressWidth from Win32_Processor");  
  63.             ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);  
  64.             ManagementObjectCollection oReturnCollection = oSearcher.Get();  
  65.             string addressWidth = null;  
  66.   
  67.             foreach (ManagementObject oReturn in oReturnCollection)  
  68.                 addressWidth = oReturn["AddressWidth"].ToString();  
  69.   
  70.             return addressWidth;  
  71.         }  
  72.   
  73.         //判断控件是否已经注册  
  74.         private static bool IsRegistered(String CLSID)  
  75.         {  
  76.             if (String.IsNullOrEmpty(CLSID))  
  77.                 return false;  
  78.   
  79.             String key = String.Format(@"CLSID\{{{0}}}", CLSID);  
  80.             RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);  
  81.             if (regKey != null)  
  82.                 return true;  
  83.             else  
  84.                 return false;  
  85.         }  
  86.     }  
  87. }  

 

源代码下载地址:点击打开链接


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值