如何以编程方式在Windows 10中设置默认浏览器

我希望能够通过C#程序更改默认浏览器(和其他关联),类似于浏览器具有“将浏览器设为默认值”选项的方式。

我已经尝试更改HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Windows \ Shell \ Associations \ UrlAssociations \ http \ UserChoice但Windows只是检测到篡改,即使我将Hash和ProgId恢复到以前的值。 似乎Hash是独一无二的,基于时间的

您可以通过运行我编写的名为Set Default Browser的小程序来完成此操作。

从MSDN文档中 ,调用SHChangeNotify以更改默认浏览器。

void NotifySystemOfNewRegistration() { SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_DWORD | SHCNF_FLUSH, nullptr, nullptr); Sleep(1000); } 

如果您想手动更改特定浏览器的注册表项,则可能需要更改这些键。

  • HKEY_CLASSES_ROOT.htm
  • HKEY_CLASSES_ROOT.html
  • HKEY_CLASSES_ROOT \ HTTP \壳\开放\命令
  • HKEY_CLASSES_ROOT \ HTTP \壳\打开\ ddeexec \应用
  • HKEY_CLASSES_ROOT \ FTP \壳\开放\命令
  • HKEY_CLASSES_ROOT \ FTP \壳\开放\ ddeexec \应用
  • HKEY_CLASSES_ROOT \鼠\壳\开放\命令
  • HKEY_CLASSES_ROOT \鼠\壳\开放\ ddeexec \应用

这些密钥来自MSDN

有关使用DISM更改Windows 10的默认浏览器的各种文章(例如来自: https : //community.spiceworks.com/topic/1812853-export-xml-with-dism-then-import-with-powershell )。 本质上,这涉及使用另一个字符串(例如,使用IE)替换文件“C:\ Windows \ System32 \ OEMDefaultAssociations.xml”中的字符串,该字符串包含来自Microsoft的原始字符串(例如,默认浏览器“Edge”)。 您可以在C#中执行相同的操作,这里我使用PowerShell执行此操作。 我知道这适用于在进行此更改后在计算机上创建的任何新用户配置文件。 文件“OEMDefaultAssociations.xml”的作用类似于新配置文件的配置文件。

 #' IE_ratherThan_Edge_MakeDefaultForNewUserProfiles_inWin10.ps1 #' From: https://community.spiceworks.com/topic/1812853-export-xml-with-dism-then-import-with-powershell #'======================================================== #' This is good for Win10 Build 1709 and 1803 #'Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine $file_OriginalWithEdge = "C:\Windows\System32\OEMDefaultAssociations.xml" $file_CopyToKeepInCase = "C:\Windows\System32\OEMDefaultAssociations_ORIG_Edge_BEFORE_replacingWithIE.xml" $file_Modified_BrowserIE = "C:\Windows\System32\OEMDefaultAssociations_Modified_with_IEratherThanEdge.xml" #' 1] Copy and rename the file Copy-Item $file_OriginalWithEdge -Destination $file_CopyToKeepInCase $stringForEdge_1 = '' $stringForIE_1 = '' $stringForEdge_2 = '' $stringForIE_2 = '' $stringForEdge_3 = '' $stringForIE_3 = '' $stringForEdge_4 = '' $stringForIE_4 = '' #' 2] Replace the string $content = [System.IO.File]::ReadAllText($file_OriginalWithEdge).Replace($stringForEdge_1,$stringForIE_1).Replace($stringForEdge_2,$stringForIE_2).Replace($stringForEdge_3,$stringForIE_3).Replace($stringForEdge_4,$stringForIE_4) [System.IO.File]::WriteAllText($file_Modified_BrowserIE, $content) #' 3] Delete the original File Remove-Item -LiteralPath $file_OriginalWithEdge #' 4] Copy the modified file to the name of the original file Copy-Item $file_Modified_BrowserIE -Destination $file_OriginalWithEdge