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

时间:2016-04-26 13:00:08

标签: c# file-type

我希望能够通过C#程序更改默认浏览器(和其他关联),类似于浏览器具有"使浏览器默认为"选项。

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

2 个答案:

答案 0 :(得分:2)

你可以通过运行我编写的名为Set Default Browser的小程序来完成这项工作。

答案 1 :(得分:0)

关于使用DISM更改Windows 10的默认浏览器的文章很多,例如,发件人:https://community.spiceworks.com/topic/1812853-export-xml-with-dism-then-import-with-powershell。从本质上讲,这涉及将文件“ C:\ Windows \ System32 \ OEMDefaultAssociations.xml”中的字符串替换为另一个字符串(例如IE),其中包含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 = '<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