欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 从注册表判断指定ocx控件是否已注册 以及获取它的注册路径

程序员文章站 2022-11-05 11:53:51
/// /// 注册控件 /// /// public bool RegControl() { try { //判断该控件是否已经注册 if (!CheckRegistredOcx(@"CLSID\{00460182-9E5E-11D5-B7C8-B8269041DD57}")) { string ... ......
 

 

 

/// <summary>
/// 注册控件
/// </summary>
/// <returns></returns>
public bool regcontrol()
{
try
{
//判断该控件是否已经注册
if (!checkregistredocx(@"clsid\{00460182-9e5e-11d5-b7c8-b8269041dd57}"))
{
string spath = path.combine(workspace.publicdirectory, "dsoframer.ocx");
if (!file.exists(spath)) return false;
process p = new process();
p.startinfo.filename = "regsvr32.exe";
p.startinfo.arguments = "/s " + spath;
p.start();
}
return true;
}
catch (exception ex)
{
logger.write(loggerlevel.error, "注册dsoframer.ocx失败" + ex.message, ex.stacktrace);
return false;
}
}

/// <summary>
/// 检测ocx是否注册
/// </summary>
/// <param name="classid"></param>
/// <returns></returns>
private bool checkregistredocx(string classid)
{
microsoft.win32.registrykey regkey = microsoft.win32.registry.classesroot.opensubkey(classid);
if (regkey != null)
{
string res = regkey.opensubkey("inprocserver32").getvalue("").tostring();
logger.write(loggerlevel.error, "已注册dsoframer.ocx控件", "注册路径:" + res);
return true;
}
else
{
logger.write(loggerlevel.error, "未注册dsoframer.ocx控件", "");
return false;
}
}