C#中的文件扩展名关联出现问题

时间:2018-07-07 16:53:58

标签: c# winforms file regedit

我做了一个媒体播放器,现在我希望mp3,wav和ogg文件的默认应用程序是我的应用程序。

我尝试使用以下代码来做到这一点,但出现错误:

    private static bool IsAssociated()
    {
        return Registry.CurrentUser.OpenSubKey( "Software\\Classes\\.mp3", false ) == null;
    }

    private static void Associate()
    {
        RegistryKey FileReg = Registry.CurrentUser.CreateSubKey( "Software\\Classes\\.mp3" );
        RegistryKey AppReg = Registry.CurrentUser.CreateSubKey( "Software\\Classes\\Applications\\BeatMusicPlayer.exe" );
        RegistryKey AppAssoc = Registry.CurrentUser.CreateSubKey( "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.mp3" );

        FileReg.CreateSubKey( "DefaultIcon" ).SetValue( "", "" );
        FileReg.CreateSubKey( "PerceivedType" ).SetValue( "", "Audio" );

        AppReg.CreateSubKey( "shell\\open\\command" ).SetValue( "", "\"" + Application.ExecutablePath + "\" %1" );
        AppReg.CreateSubKey( "DefaultIcon" ).SetValue( "", "" );

        AppAssoc.CreateSubKey( "UserChoice" ).SetValue( "Progid", "Applications\\BeatMusicPlayer.exe" );
    }

    static void Main( string[] args )
    {
        if( !IsAssociated() )
            Associate();
    }

但在AppAssoc.CreateSubKey( "UserChoice" ).SetValue( "Progid", "Applications\\BeatMusicPlayer.exe" );行中,我成为例外System.UnauthorizedAccessException

我试图以管理员身份运行该程序,但没有任何改变。

亲切的问候

0 个答案:

没有答案