Skip to content

Commit

Permalink
Add ability to open hosts file in default text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlerch committed Oct 22, 2016
1 parent d873c31 commit 97f0b1f
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 16 deletions.
8 changes: 5 additions & 3 deletions HostsFileEditor.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostsFileEditor", "src\HostsFileEditor.csproj", "{17A37069-B2AC-4D58-8F6E-C955F284F94C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sources", "Sources", "{F4971D95-21F4-4E91-B8EE-3971F0B50FE3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{C802A54D-0F13-4EE0-B112-D9E55E3854F1}"
ProjectSection(SolutionItems) = preProject
License.txt = License.txt
Readme.txt = Readme.txt
License.md = License.md
Readme.md = Readme.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{BF6F8492-4872-444A-B62F-000A90D73EA4}"
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Release binaries can be downloaded from [CodePlex](http://hostsfileeditor.codepl

## Build

Requires Windows SDK for Windows 7 and .NET or Visual Studio 2010 or newer. To build the installer you must have WiX Toolset 3.7+.
Requires Visual Studio 2015 or newer. To build the installer you must have WiX Toolset 3.7+.

Run `build-release.bat` or `build-debug.bat`.

Expand Down
2 changes: 1 addition & 1 deletion build-clean.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if exist logs goto msbuild
mkdir logs

:msbuild
%windir%\Microsoft.NET\Framework\v4.0.30319\MSBUILD.exe HostsFileEditor.proj /t:Clean
"%PROGRAMFILES(X86)%\MSBuild\14.0\Bin\MSBUILD.exe" HostsFileEditor.proj /t:Clean
2 changes: 1 addition & 1 deletion build-debug.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if exist logs goto msbuild
mkdir logs

:msbuild
%windir%\Microsoft.NET\Framework\v4.0.30319\MSBUILD.exe HostsFileEditor.proj /t:Build /p:Configuration=Debug /l:FileLogger,Microsoft.Build.Engine;logfile=logs\build-debug.log
"%PROGRAMFILES(X86)%\MSBuild\14.0\Bin\MSBUILD.exe" HostsFileEditor.proj /t:Build /p:Configuration=Debug /l:FileLogger,Microsoft.Build.Engine;logfile=logs\build-debug.log
2 changes: 1 addition & 1 deletion build-release.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if exist logs goto msbuild
mkdir logs

:msbuild
%windir%\Microsoft.NET\Framework\v4.0.30319\MSBUILD.exe HostsFileEditor.proj /t:Build /p:Configuration=Release /l:FileLogger,Microsoft.Build.Engine;logfile=logs\build-release.log
"%PROGRAMFILES(X86)%\MSBuild\14.0\Bin\MSBUILD.exe" HostsFileEditor.proj /t:Build /p:Configuration=Release /l:FileLogger,Microsoft.Build.Engine;logfile=logs\build-release.log
4 changes: 4 additions & 0 deletions setup/NGenInstallCustomAction/NGenCustomAction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -31,6 +33,8 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
6 changes: 3 additions & 3 deletions setup/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="{EC9CF3E9-3C14-43D6-B9D0-5B4232926FAC}" Codepage="1252" Language="1033" Manufacturer="Scott Lerch" Name="Hosts File Editor" UpgradeCode="{3A4F4E01-1C91-4562-8C04-5F4273501698}" Version="1.0.0">
<Product Id="{23EEACB7-C78A-4334-900C-A260372997E2}" Codepage="1252" Language="1033" Manufacturer="Scott Lerch" Name="Hosts File Editor" UpgradeCode="{3A4F4E01-1C91-4562-8C04-5F4273501698}" Version="1.1.0">
<Package Comments="Edit and manager your Windows hosts file." Compressed="yes" InstallerVersion="200" Keywords="hosts" Languages="1033" Manufacturer="Scott Lerch" Platform="x86" />
<MajorUpgrade DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit." />
<Binary Id="InstallUtil" SourceFile="Resources\Binary\InstallUtil" />
<Binary Id="MSVBDPCADLL" SourceFile="Resources\Binary\MSVBDPCADLL" />
<Binary Id="VSDNETCFG" SourceFile="Resources\Binary\VSDNETCFG" />
Expand Down Expand Up @@ -714,7 +715,7 @@
</UI>
<Upgrade Id="{3A4F4E01-1C91-4562-8C04-5F4273501698}">
<UpgradeVersion Maximum="1.0.0" Property="PREVIOUSVERSIONSINSTALLED" />
<UpgradeVersion Minimum="1.0.0" Property="NEWERPRODUCTFOUND" OnlyDetect="yes" IncludeMinimum="yes" />
<UpgradeVersion Minimum="1.1.0" Property="NEWERPRODUCTFOUND" OnlyDetect="yes" IncludeMinimum="yes" />
</Upgrade>
<InstallExecuteSequence>
<Custom Action="DIRCA_CheckFX" Sequence="1" />
Expand Down Expand Up @@ -785,7 +786,6 @@
<PublishFeatures Sequence="6300" />
<PublishProduct Sequence="6400" />
<InstallFinalize Sequence="6600" />
<RemoveExistingProducts Sequence="6550" />
<InstallExecute Sequence="6500" />
<MsiPublishAssemblies Sequence="6250" />
<MsiUnpublishAssemblies Sequence="1650" />
Expand Down
6 changes: 2 additions & 4 deletions src/HostsFileEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
<RunCodeAnalysis>true</RunCodeAnalysis>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
Expand All @@ -53,9 +52,7 @@
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RunCodeAnalysis>true</RunCodeAnalysis>
Expand Down Expand Up @@ -131,6 +128,7 @@
<Compile Include="ProgramSingleInstance.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\FileEx.cs" />
<Compile Include="Utilities\FileOpener.cs" />
<Compile Include="Utilities\UndoManager.cs" />
<Compile Include="Utilities\Reflect.cs" />
<Compile Include="Win32\NativeMethods.cs" />
Expand Down
11 changes: 11 additions & 0 deletions src/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,16 @@ private void OnAboutClick(object sender, EventArgs e)
(new AboutForm()).ShowDialog(this);
}

/// <summary>
/// Called when open text editor clicked.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void OnOpenTextEditorClick(object sender, EventArgs e)
{
FileOpener.OpenTextFile(HostsFile.DefaultHostFilePath);
}

#endregion
}
}
12 changes: 12 additions & 0 deletions src/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4862,4 +4862,16 @@
<data name="&gt;&gt;$this.Type" xml:space="preserve">
<value>System.Windows.Forms.Form, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="openTextEditor.Size" type="System.Drawing.Size, System.Drawing">
<value>166, 22</value>
</data>
<data name="openTextEditor.Text" xml:space="preserve">
<value>Open &amp;Text Editor</value>
</data>
<data name="&gt;&gt;openTextEditor.Name" xml:space="preserve">
<value>openTextEditor</value>
</data>
<data name="&gt;&gt;openTextEditor.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: NeutralResourcesLanguage("en-US")]
60 changes: 60 additions & 0 deletions src/Utilities/FileOpener.cs
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();
}
}
}
}

0 comments on commit 97f0b1f

Please sign in to comment.