[Hosts] Open hosts file in default editor (#21284)

* open hosts file

* open with default editor
This commit is contained in:
Davide Giacometti 2022-10-24 20:09:00 +02:00 коммит произвёл GitHub
Родитель 4da866aaa3
Коммит 2add9db780
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 94 добавлений и 1 удалений

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
@ -14,6 +15,7 @@ using System.Threading;
using System.Threading.Tasks;
using Hosts.Models;
using Hosts.Settings;
using Microsoft.Win32;
using Settings.UI.Library.Enumerations;
namespace Hosts.Helpers
@ -210,6 +212,69 @@ namespace Hosts.Helpers
.ForEach(f => f.Delete());
}
public void OpenHostsFile()
{
var notepadFallback = false;
try
{
// Try to open in default editor
var key = Registry.ClassesRoot.OpenSubKey("SystemFileAssociations\\text\\shell\\edit\\command");
if (key != null)
{
var commandPattern = key.GetValue(string.Empty).ToString(); // Default value
var file = null as string;
var args = null as string;
if (commandPattern.StartsWith('\"'))
{
var endQuoteIndex = commandPattern.IndexOf('\"', 1);
if (endQuoteIndex != -1)
{
file = commandPattern[1..endQuoteIndex];
args = commandPattern[(endQuoteIndex + 1)..].Trim();
}
}
else
{
var spaceIndex = commandPattern.IndexOf(' ');
if (spaceIndex != -1)
{
file = commandPattern[..spaceIndex];
args = commandPattern[(spaceIndex + 1)..].Trim();
}
}
if (file != null && args != null)
{
args = args.Replace("%1", HostsFilePath);
Process.Start(new ProcessStartInfo(file, args));
}
else
{
notepadFallback = true;
}
}
}
catch (Exception ex)
{
Logger.LogError("Failed to open default editor", ex);
notepadFallback = true;
}
if (notepadFallback)
{
try
{
Process.Start(new ProcessStartInfo("notepad.exe", HostsFilePath));
}
catch (Exception ex)
{
Logger.LogError("Failed to open notepad", ex);
}
}
}
public void Dispose()
{
Dispose(disposing: true);

Просмотреть файл

@ -22,5 +22,7 @@ namespace Hosts.Helpers
Task<bool> PingAsync(string address);
void CleanupBackup();
void OpenHostsFile();
}
}

Просмотреть файл

@ -222,6 +222,14 @@
<data name="MoveUp.Text" xml:space="preserve">
<value>Move up</value>
</data>
<data name="OpenHostsFileBtn.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
<value>Open hosts file</value>
<comment>"Hosts" refers to the system hosts file, do not loc</comment>
</data>
<data name="OpenHostsFileBtn.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Open hosts file</value>
<comment>"Hosts" refers to the system hosts file, do not loc</comment>
</data>
<data name="Ping.Text" xml:space="preserve">
<value>Ping</value>
<comment>"Ping" refers to the command-line utility, do not loc</comment>

Просмотреть файл

@ -97,6 +97,8 @@ namespace Hosts.ViewModels
public ICommand OpenSettingsCommand => new RelayCommand(OpenSettings);
public ICommand OpenHostsFileCommand => new RelayCommand(OpenHostsFile);
public MainViewModel(
IHostsService hostService,
IUserSettings userSettings)
@ -197,6 +199,11 @@ namespace Hosts.ViewModels
SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts);
}
public void OpenHostsFile()
{
_hostsService.OpenHostsFile();
}
public void Dispose()
{
Dispose(disposing: true);

Просмотреть файл

@ -124,7 +124,18 @@
</Flyout>
</Button.Flyout>
</Button>
<Button
x:Uid="OpenHostsFileBtn"
Height="36"
Command="{x:Bind ViewModel.OpenHostsFileCommand}"
Style="{StaticResource SubtleButtonStyle}">
<FontIcon
FontFamily="{StaticResource SymbolThemeFontFamily}"
FontSize="14"
Glyph="&#xe8a7;" />
</Button>
<Button
x:Uid="SettingsBtn"
Height="36"