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

 

 

 

/// <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;
}
}

 

 

posted @ 2018-10-11 13:53 衡宇ing 阅读(...) 评论(...) 编辑 收藏