我想在我的电脑上使用C#获取所有progis的列表。我知道我可以扫描注册表,这样的事情:列出所有可用ProgId
var regClis = Registry.ClassesRoot.OpenSubKey("CLSID");
var progs = new List<string>();
foreach (var clsid in regClis.GetSubKeyNames()) {
var regClsidKey = regClis.OpenSubKey(clsid);
var ProgID = regClsidKey.OpenSubKey("ProgID");
var regPath = regClsidKey.OpenSubKey("InprocServer32");
if (regPath == null)
regPath = regClsidKey.OpenSubKey("LocalServer32");
if (regPath != null && ProgID != null) {
var pid = ProgID.GetValue("");
var filePath = regPath.GetValue("");
progs.Add(pid + " -> " + filePath);
regPath.Close();
}
regClsidKey.Close();
}
的代码是从this blog.
有谁知道,如果有一个更简单的方法?另外,据我了解这是如何工作的,上面的代码很可能只会给我在进程内的com。如果我还需要进程progid以及怎么办?
在此先感谢
'LocalServer32'是用于进程外的。它不能比直接枚举更简单。 –
如果你扫描使用如下代码注册表,请记住,'RegistryKey'是'IDisposable' - 你应该'Dispose'每个对象从以'OpenSubKey'的调用返回,否则会消耗大量的注册表手柄,直到垃圾当终结器线程轮到关闭它们时收集。 –