mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
[Hosts] Open hosts file in default editor (#21284)
* open hosts file * open with default editor
This commit is contained in:
committed by
GitHub
parent
4da866aaa3
commit
2add9db780
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Abstractions;
|
using System.IO.Abstractions;
|
||||||
@@ -14,6 +15,7 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Hosts.Models;
|
using Hosts.Models;
|
||||||
using Hosts.Settings;
|
using Hosts.Settings;
|
||||||
|
using Microsoft.Win32;
|
||||||
using Settings.UI.Library.Enumerations;
|
using Settings.UI.Library.Enumerations;
|
||||||
|
|
||||||
namespace Hosts.Helpers
|
namespace Hosts.Helpers
|
||||||
@@ -210,6 +212,69 @@ namespace Hosts.Helpers
|
|||||||
.ForEach(f => f.Delete());
|
.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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
|
|||||||
@@ -22,5 +22,7 @@ namespace Hosts.Helpers
|
|||||||
Task<bool> PingAsync(string address);
|
Task<bool> PingAsync(string address);
|
||||||
|
|
||||||
void CleanupBackup();
|
void CleanupBackup();
|
||||||
|
|
||||||
|
void OpenHostsFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,6 +222,14 @@
|
|||||||
<data name="MoveUp.Text" xml:space="preserve">
|
<data name="MoveUp.Text" xml:space="preserve">
|
||||||
<value>Move up</value>
|
<value>Move up</value>
|
||||||
</data>
|
</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">
|
<data name="Ping.Text" xml:space="preserve">
|
||||||
<value>Ping</value>
|
<value>Ping</value>
|
||||||
<comment>"Ping" refers to the command-line utility, do not loc</comment>
|
<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 OpenSettingsCommand => new RelayCommand(OpenSettings);
|
||||||
|
|
||||||
|
public ICommand OpenHostsFileCommand => new RelayCommand(OpenHostsFile);
|
||||||
|
|
||||||
public MainViewModel(
|
public MainViewModel(
|
||||||
IHostsService hostService,
|
IHostsService hostService,
|
||||||
IUserSettings userSettings)
|
IUserSettings userSettings)
|
||||||
@@ -197,6 +199,11 @@ namespace Hosts.ViewModels
|
|||||||
SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts);
|
SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OpenHostsFile()
|
||||||
|
{
|
||||||
|
_hostsService.OpenHostsFile();
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Dispose(disposing: true);
|
Dispose(disposing: true);
|
||||||
|
|||||||
@@ -125,6 +125,17 @@
|
|||||||
</Button.Flyout>
|
</Button.Flyout>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
x:Uid="OpenHostsFileBtn"
|
||||||
|
Height="36"
|
||||||
|
Command="{x:Bind ViewModel.OpenHostsFileCommand}"
|
||||||
|
Style="{StaticResource SubtleButtonStyle}">
|
||||||
|
<FontIcon
|
||||||
|
FontFamily="{StaticResource SymbolThemeFontFamily}"
|
||||||
|
FontSize="14"
|
||||||
|
Glyph="" />
|
||||||
|
</Button>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
x:Uid="SettingsBtn"
|
x:Uid="SettingsBtn"
|
||||||
Height="36"
|
Height="36"
|
||||||
|
|||||||
Reference in New Issue
Block a user