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

9

我希望能够通过类似浏览器的“设置为默认浏览器”选项的C#程序来更改默认浏览器(和其他关联)。

我尝试过更改HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice,但是Windows会检测到篡改,即使我将Hash和ProgId都恢复到先前的值。似乎Hash是唯一的、基于时间的。


请停止干扰他人的电脑。浏览器选择是用户的选择。 - Phil1970
2
告诉微软吧,我的默认浏览器是Chrome,绘图工具是Paint NET,播放器是Daum Player,但他们仍然每隔几个月就会问:“应该用什么打开这个文件?”推荐使用Edge、Paint或Video。 - allanx2000
2
@Phil1970如果他/她正在编写一个程序,允许用户更改他们的默认浏览器,该怎么办? - rory.ap
3个回答

2

2
有多种文章介绍使用DISM更改Windows 10的默认浏览器(例如:https://community.spiceworks.com/topic/1812853-export-xml-with-dism-then-import-with-powershell)。本质上,这涉及将包含来自Microsoft的原始字符串(例如默认浏览器'Edge')的文件“C:\ Windows \ System32 \ OEMDefaultAssociations.xml”中的字符串替换为另一个字符串(例如IE)。您可以在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 = '<Association Identifier=".htm" ProgId="AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9" ApplicationName="Microsoft Edge" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppX6k1pws1pa7jjhchyzw9jce3e6hg6vn8d" />'
$stringForIE_1 = '<Association Identifier=".htm" ProgId="htmlfile" ApplicationName="Internet Explorer" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9" />'

$stringForEdge_2 = '<Association Identifier=".html" ProgId="AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9" ApplicationName="Microsoft Edge" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppX6k1pws1pa7jjhchyzw9jce3e6hg6vn8d" />'
$stringForIE_2 = '<Association Identifier=".html" ProgId="htmlfile" ApplicationName="Internet Explorer" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppX4hxtad77fbk3jkkeerkrm0ze94wjf3s9" />'

$stringForEdge_3 = '<Association Identifier="http" ProgId="AppXq0fevzme2pys62n3e0fbqa7peapykr8v" ApplicationName="Microsoft Edge" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppXehk712w0hx4w5b8k25kg808a9h84jamg" />'
$stringForIE_3 = '<Association Identifier="http" ProgId="IE.HTTP" ApplicationName="Internet Explorer" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppXq0fevzme2pys62n3e0fbqa7peapykr8v" />'

$stringForEdge_4 = '<Association Identifier="https" ProgId="AppX90nv6nhay5n6a98fnetv7tpk64pp35es" ApplicationName="Microsoft Edge" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppXz8ws88f5y0y5nyrw1b3pj7xtm779tj2t" />'
$stringForIE_4 = '<Association Identifier="https" ProgId="IE.HTTPS" ApplicationName="Internet Explorer" ApplyOnUpgrade="true" OverwriteIfProgIdIs="AppX90nv6nhay5n6a98fnetv7tpk64pp35es" />'



#' 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

0
确实,微软正在试图阻止通过不公开用于创建所需的Hash值的哈希算法来编程更改默认的网络浏览器的能力,并与相关注册表键中的值相匹配。
然而,一个第三方工具SetDefaultBrowser.exe已经找到了这个算法,因此允许完全自动化地更改用户的默认网络浏览器。
  • 有关背景信息和下载链接,请参阅此博文

    • 注意:链接的博文中有一个关于需要便携式浏览器安装的过时警告,可以安全地忽略它。
  • 或者,如果您已经安装了Chocolatey,您可以按照以下步骤安装SetDefaultBrowser.exe

    choco install setdefaultbrowser -y
    

@DavidMaze,链接帖子的关键部分就在回答中。这个回答的价值在于提供一个高层次的解释,说明为什么除非你拥有内部知识,否则解决方案是不可能的,并且指出了一个可下载的实用程序(附带详细背景信息),该实用程序封装了这个内部知识。我后来了解到这个实用程序也可以通过 Chocolatey 下载,所以我添加了另一个下载链接。 - mklement0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接