我做了一个媒体播放器,现在我希望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
我试图以管理员身份运行该程序,但没有任何改变。
亲切的问候