XHost is a command line tool to manage Windows hosts file.
.NET Framework 3.5+
- Add the containing directory of
xhost.exe
to PATH environment variable; - Open command line window (cmd.exe), type
xhost -help
;
xhost -ls
: List all host entries;xhost -find test.com
: Find the entry whose host istest.com
;xhost -add 127.0.0.1 test.com
: Add a new entry to the hosts file;xhost -update 127.0.0.1 test.com
: Update thetest.com
entry;xhost -rm test1.com
: Remove hosttest1.com
from the hosts file;xhost -rm test1.com, test2.com
: Remove hosttest1.com
andtest2.com
from the hosts file;xhost -h
: Show help;
Custom commands can be added by writing plugins. A plugin in XHost is a class implementing XHost.Commands.ICommand
. To write a plugin, you have to:
- Write a class implementing
XHost.Commands.ICommand
interface; - Compile the plugin;
- Drop the dll file to the
Plugins
folder (please create it if not exists);
NuGet is sometimes blocked in China. In this sample plugin, we will add a host entry for nuget.org.
public class NuGetCommand : ICommand
{
public string Name
{
get { return "nuget"; }
}
public string ShortName
{
get { return null; }
}
public string Description
{
get { return "Add 157.56.8.150 nuget.org to hosts file."; }
}
public string Usage
{
get { return "xhost -nuget"; }
}
public void Execute(CommandLine commandLine, CommandExecutionContext context)
{
context.Hosts.Set("157.56.8.150", "nuget.org");
context.Hosts.Save();
Console.WriteLine("OK");
}
}
Note: The Description
and Usage
properties will be used in the help.
Then you can use xhost -nuget
to add host entry for nuget.org.