2015-02-08 23 views
3

我試圖設置一種方法來管理我的程序在C#中的文件關聯。我已經使用WiX在註冊表中設置了正確的值,並且找到了ApplicationAssociationRegistrationUI的封裝器,它允許我打開GUI來設置文件關聯。但它不起作用。我收到以下異常:找不到元素。 (從HRESULT異常:0x80070490)C#中的LaunchAdvancedAssociationUI - >在Windows 8上找不到的元素

封套:

namespace FileAssociation 
{ 
    [ClassInterface(ClassInterfaceType.None)] 
    [ComImport] 
    [Guid("1968106d-f3b5-44cf-890e-116fcb9ecef1")] 
    [TypeLibType(TypeLibTypeFlags.FCanCreate)] 
    public sealed class ApplicationAssociationRegistrationUI : IApplicationAssociationRegistrationUI 
    { 
     [MethodImpl(MethodImplOptions.InternalCall)] 
     public extern void LaunchAdvancedAssociationUI(string appRegistryName); 
    } 

    [CoClass(typeof(ApplicationAssociationRegistrationUI))] 
    [ComImport] 
    [Guid("1f76a169-f994-40ac-8fc8-0959e8874710")] 
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    [TypeLibImportClass(typeof(ApplicationAssociationRegistrationUI))] 
    public interface IApplicationAssociationRegistrationUI 
    { 
     void LaunchAdvancedAssociationUI([MarshalAs(UnmanagedType.LPWStr)] string appRegistryName); 
    } 
} 

用法:

var assocUi = new ApplicationAssociationRegistrationUI(); 
try 
{ 
    assocUi.LaunchAdvancedAssociationUI(InstanceManager.ProgId); 
} 
catch 
{ 
    MessageBox.Show("Could not display the file association manager. Please repair the installation and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); 
} 
finally 
{ 
    Marshal.ReleaseComObject(assocUi); 
} 

再次,在註冊表中存在的所有正確的鍵。這不是COM Interop第一次爲我失敗,所以我開始認爲我必須錯過重要的東西。我嘗試在項目屬性中檢查「註冊COM Interop」,並嘗試使其可見。

我知道這隻適用於Vista或更新,這是好的,因爲我的程序不支持XP。我在Windows 8.1上測試它,作爲管理員和普通用戶。

編輯:它適用於Windows 7!在MSDN上並沒有說這個API在Win8中被棄用...

我做了什麼錯了?有沒有更簡單的方法來做到這一點,我不知道?

回答

1

終於發現問題了!!! 從Windows 8開始,該程序需要有公司的信息(我認爲這是一個錯誤,因爲它不是在微軟的網站上提及。) 因此,請務必填寫此屬性中的AssemblyInfo.cs:

[assembly: AssemblyCompany("YourCompany")] 

如果它是一個空字符串,它將無法工作!