我認爲問題和答案都是錯誤的。這裏涉及三個名稱:
- ClassId或GUID,具有熟悉的格式,例如, 「{F4754C9B-64F5-4B40-8AF4-679732AC0607}」
- ProgId或程序名稱,應格式化爲Vendor.Component.Version(請參閱http://msdn.microsoft.com/en-us/library/aa911706.aspx) - 例如「Word.Document.12」
- 人類可讀的名稱 - 這是通常顯示在COM組件列表中的名稱(如VS中的添加引用/ COM對話框或OLE應用程序中的插入 - >對象菜單),例如「Microsoft Word文檔」
在註冊表中的相應條目爲:
CLASSES_ROOT\CLSID\ClassId @= "Human readable name"
CLASSES_ROOT\CLSID\ClassId\ProgId @= "ProgId"
CLASSES_ROOT\ProgId @= "Human readable name"
CLASSES_ROOT\ProgId\CLSID @= "ClassId"
看看你自己的註冊表中的一些例子。
如果您反思regasm.exe,您會發現它始終使用Type.FullName作爲人類可讀的名稱。這是一個痛苦的屁股,因爲這意味着你必須使用com註冊掛鉤來設置HRN。例如,他們很容易查找DisplayName屬性,然後使用它。無論如何,這是一個設置HRN條目的幫助函數。從您的註冊掛鉤調用它。
有關COM註冊鉤更多細節參見http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comregisterfunctionattribute.aspx
public static void SetHumanReadableName(Type t, string humanReadableName)
{
var classKeyName = string.Format(@"CLSID\{{{0}}}", t.GUID);
var classKey = Registry.ClassesRoot.OpenSubKey(classKeyName, true);
if (classKey == null)
return;
classKey.SetValue(string.Empty, humanReadableName);
// if it has a progid set the HRN on the progid entry too
foreach (ProgIdAttribute progId in t.GetCustomAttributes(typeof(ProgIdAttribute), false))
{
var progIdKey = Registry.ClassesRoot.OpenSubKey(progId.Value, true);
if (progIdKey != null)
progIdKey.SetValue(string.Empty, humanReadableName);
}
}