C# dsoframer.ocx 控件注册

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

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

  

代码如下:

   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Management;

namespace dsoframerRegister
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileFullPath = string.Format("{0}\\dsoframer.ocx", System.Environment.CurrentDirectory);
            string systemDrive = System.Environment.GetEnvironmentVariable("systemdrive");//获取系统所在的盘符
            if (!File.Exists(fileFullPath) || String.IsNullOrEmpty(systemDrive))
                return;

            bool isRegisted = IsRegistered("00460182-9E5E-11D5-B7C8-B8269041DD57");
            if (isRegisted)
                return;


            string windowsPath = string.Empty;

            if (GetOSBitCount() == "64")
                windowsPath = string.Format("{0}\\Windows\\SysWOW64", systemDrive);
            else
                windowsPath = string.Format("{0}\\Windows\\System32", systemDrive);

            if (!Directory.Exists(windowsPath))
                return;


            File.Copy(fileFullPath, string.Format("{0}\\dsoframer.ocx", windowsPath), true);

            Registe(string.Format("{0}\\dsoframer.ocx", windowsPath));

        }

        //注册dsoframer.ocx
        private static bool Registe(string fileFullName)
        {
            bool result = false;
            System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");//注册完毕不显示是否成功的提示
            //System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName);//注册完毕显示是否成功的提示
            if (p != null && p.HasExited)
            {
                Int32 exitCode = p.ExitCode;
                if (exitCode == 0)
                    result = true;
            }
            return result;
        }

        //获取当前操作系统的位数
        private static string GetOSBitCount()
        {
            ConnectionOptions oConn = new ConnectionOptions();
            System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost", oConn);
            System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select AddressWidth from Win32_Processor");
            ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
            ManagementObjectCollection oReturnCollection = oSearcher.Get();
            string addressWidth = null;

            foreach (ManagementObject oReturn in oReturnCollection)
                addressWidth = oReturn["AddressWidth"].ToString();

            return addressWidth;
        }

        //判断控件是否已经注册
        private static bool IsRegistered(String CLSID)
        {
            if (String.IsNullOrEmpty(CLSID))
                return false;

            String key = String.Format(@"CLSID\{{{0}}}", CLSID);
            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);
            if (regKey != null)
                return true;
            else
                return false;
        }
    }
}

 

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值