hosts.net 1.0.3

dotnet add package hosts.net --version 1.0.3
NuGet\Install-Package hosts.net -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="hosts.net" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add hosts.net --version 1.0.3
#r "nuget: hosts.net, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install hosts.net as a Cake Addin
#addin nuget:?package=hosts.net&version=1.0.3

// Install hosts.net as a Cake Tool
#tool nuget:?package=hosts.net&version=1.0.3

hosts.net

Nuget

hosts.net is a simple library that enables developers to easily develop applications that can programmatically manipulate the hosts file across various platforms.

installation

dotnet add package hosts.net

usage

Editing the hosts file on unix requires sudo permission, and likewise administrator is required on Windows.

On Unix-like Systems: hosts.net will default to the /etc/hosts file.

On Windows Systems: the library will default to the C:/Windows/System32/drivers/etc/hosts file. The library automatically handles Windows' Access Control List. If any issues arise from this please file an issue.

Demo Code:

using System.Linq;
using System.Net;
using hosts.net;

// open the file
var file = Hosts.OpenHostsFile(/* optional file path */);

// use linq to query the entries to prevent duplicate entries

if (file.Entries.Any(entry => entry.Comment is not null && entry.Comment == "Added with hosts.net")) return;

file.AddBlankEntry()
    .SetComment("Added with hosts.net");

file.AddBlankEntry()
    .SetHost(/* address */ IPAddress.Loopback, /* canonical hostname */ "non-existent.domain.n", /* optional aliases */ "non-existent.alias.n");

// other code here...

// save to the disk
file.Write();


// Result:

// # Added with hosts.net
// 127.0.0.1 non-existent.domain.n non-existent.alias.n

Removing an entry with LINQ:

file.Entries.RemoveAll(entry => entry.CanonicalHostname == "encodeous.ca");

Adding an empty line:

file.AddBlankEntry();

Reading & Printing all of the lines:

Console.WriteLine(string.Join('\n', 
    file.Entries.Select(x => x.RawString))
);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 219 1/3/2024
1.0.2 765 7/16/2022
1.0.1 387 7/16/2022
1.0.0 383 7/16/2022