c# 获取 com 引用真实组件地址

1.根据guid获取

var clsid = new Guid("63EA2B90-C5A8-46F4-8A6E-2F2436C80003").ToString("B");
    var registryKey = Registry.ClassesRoot.OpenSubKey(@"Wow6432Node\CLSID\" + clsid + @"\InprocServer32");
    //32位程序与64位此处路径可能有所不同,但一般会自动跳转,具体情况还得分析。
    //HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{63EA2B90-C5A8-46F4-8A6E-2F2436C80003}\InprocServer32
    if (registryKey != null)
    {
        var classPath = registryKey.GetValue("CodeBase").ToString().Trim();
        if (!string.IsNullOrWhiteSpace(classPath))
        {
            var uri = new Uri(classPath);
            if (uri.IsFile)
            {
                Console.WriteLine(uri.LocalPath);
            }
        }
    }

2.获取注册在gac中的组件地址,用上面的方法是不行的。

var ass = Assembly.LoadFrom(@"C:\Users\Administrator\Desktop\test\FirstEliteTools.dll"); //只要guid相同就可找到真实地址
MessageBox.Show(ass.CodeBase);

猜你喜欢

转载自www.cnblogs.com/nanfei/p/10218353.html