C# 從登錄檔判斷指定ocx控制元件是否已註冊 以及獲取它的註冊路徑

衡宇ing發表於2018-10-11
 

 

 

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

 

 

相關文章