-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to open hosts file in default text editor
- Loading branch information
1 parent
d873c31
commit 97f0b1f
Showing
13 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Microsoft.Win32; | ||
using System.Diagnostics; | ||
|
||
namespace HostsFileEditor.Utilities | ||
{ | ||
/// <summary> | ||
/// Helper class to open files. | ||
/// </summary> | ||
public class FileOpener | ||
{ | ||
/// <summary> | ||
/// Open text file using default text editor. | ||
/// </summary> | ||
/// <param name="path">Full path to text file.</param> | ||
public static void OpenTextFile(string path) | ||
{ | ||
string application; | ||
if (!TryGetRegisteredApplication(".txt", out application)) | ||
{ | ||
application = "notepad.exe"; | ||
} | ||
|
||
Process.Start(application, path); | ||
} | ||
|
||
private static bool TryGetRegisteredApplication(string extension, out string registeredApp) | ||
{ | ||
var extensionId = GetClassesRootKeyDefaultValue(extension); | ||
|
||
if (extensionId == null) | ||
{ | ||
registeredApp = null; | ||
return false; | ||
} | ||
|
||
var openCommand = GetClassesRootKeyDefaultValue(extensionId + "/shell/open/command"); | ||
|
||
if (openCommand == null) | ||
{ | ||
registeredApp = null; | ||
return false; | ||
} | ||
|
||
registeredApp = openCommand | ||
.Replace("%1", string.Empty) | ||
.Replace("\"", string.Empty) | ||
.Trim(); | ||
|
||
return true; | ||
} | ||
|
||
private static string GetClassesRootKeyDefaultValue(string keyPath) | ||
{ | ||
using (var key = Registry.ClassesRoot.OpenSubKey(keyPath)) | ||
{ | ||
return key?.GetValue(null)?.ToString(); | ||
} | ||
} | ||
} | ||
} |