BornToBeRoot/NETworkManager · error · InvalidOperationException
Hosts file path is invalid.
Error message
Hosts file path is invalid.
What it means
The HostsFileEditor constructor creates a FileSystemWatcher for the hosts file and passes Path.GetDirectoryName(HostsFilePath) to the watcher's Path property. When the directory part of the configured path is null or empty, Path.GetDirectoryName returns null and the ??-fallback throws InvalidOperationException('Hosts file path is invalid.'), aborting construction of the editor.
Solutions
- Pass a fully qualified absolute path (Path.GetFullPath) as HostsFilePath.
- Keep the default %WinDir%\System32\drivers\etc\hosts location.
- Validate the path with Path.IsPathRooted and Path.HasExtension before constructing HostsFileEditor.
- Catch InvalidOperationException in the caller and fall back to the system default hosts path.
Example fix
// before
var editor = new HostsFileEditor("hosts");
// after
var hostsPath = Path.IsPathRooted(path) ? path : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts");
var editor = new HostsFileEditor(hostsPath); Defensive patterns
Strategy: validation
Validate before calling
// before constructing HostsFileEditor
if (string.IsNullOrWhiteSpace(path) || !Path.IsPathRooted(path))
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts");
if (string.IsNullOrEmpty(Path.GetDirectoryName(path)))
throw new ArgumentException("Hosts path must include a directory", nameof(path)); Type guard
null
Try / catch
try
{
var editor = new HostsFileEditor(path);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Hosts file"))
{
Log.Warn("Invalid hosts path configured, falling back to system default", ex);
var editor = new HostsFileEditor(DefaultHostsPath);
} Prevention
- Always pass fully qualified absolute paths.
- Keep the default %WinDir%\System32\drivers\etc\hosts unless the user explicitly overrides it.
- Validate configured paths in the settings UI before saving.
When it happens
Trigger: Constructing HostsFileEditor with a HostsFilePath that has no directory component (e.g. just 'hosts', a root-less relative path), or a malformed path string so GetDirectoryName returns null.
Common situations: A setting or environment override replacing the default C:\Windows\System32\drivers\etc\hosts with a bare file name or invalid string; unit tests passing a relative file name; path strings with illegal characters.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
AI-assisted analysis of BornToBeRoot/NETworkManager@2780d65469 (2026-09-12).
Data as JSON: /api/errors/3229268071432c95.
Report an issue: GitHub.
Appendix: source
Thrown at Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs:85
/// Regex to match a hosts file entry with optional comments, supporting IPv4, IPv6, and hostnames
/// </summary>
private static readonly Regex HostsFileEntryRegex = new(RegexHelper.HostsEntryRegex);
#endregion
#region Constructor
static HostsFileEditor()
{
// Create a file system watcher to monitor changes to the hosts file
try
{
Log.Debug("HostsFileEditor - Creating file system watcher for hosts file...");
// Create the file system watcher
HostsFileWatcher = new FileSystemWatcher();
HostsFileWatcher.Path = Path.GetDirectoryName(HostsFilePath) ??
throw new InvalidOperationException("Hosts file path is invalid.");
HostsFileWatcher.Filter = Path.GetFileName(HostsFilePath) ??
throw new InvalidOperationException("Hosts file name is invalid.");
HostsFileWatcher.NotifyFilter = NotifyFilters.LastWrite;
// Maybe fired twice. This is a known bug/feature.
// See: https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice
HostsFileWatcher.Changed += (_, _) => OnHostsFileChanged();
// Enable the file system watcher
HostsFileWatcher.EnableRaisingEvents = true;
Log.Debug("HostsFileEditor - File system watcher for hosts file created.");
}
catch (Exception ex)
{
Log.Error("Failed to create file system watcher for hosts file.", ex);
}
}View on GitHub (pinned to 2780d65469)