BornToBeRoot/NETworkManager · error · InvalidOperationException
Hosts file name is invalid.
Error message
Hosts file name is invalid.
What it means
In the HostsFileEditor constructor, Path.GetFileName(HostsFilePath) is assigned to the FileSystemWatcher Filter; if it returns null/empty the ??-fallback throws InvalidOperationException('Hosts file name is invalid.'). This happens when the path ends in a directory separator or is empty, leaving no file-name component.
Solutions
- Pass the complete file path including the file name (e.g. ...\etc\hosts).
- Normalize with Path.GetFullPath and verify !string.IsNullOrEmpty(Path.GetFileName(path)) before constructing.
- Validate in settings UI that the configured value is a file, not a directory.
- Catch the InvalidOperationException and revert to the default hosts path.
Example fix
// before
var editor = new HostsFileEditor(@"C:\Windows\System32\drivers\etc\");
// after
var p = Path.GetFullPath(path);
if (string.IsNullOrEmpty(Path.GetFileName(p)))
p = Path.Combine(p, "hosts");
var editor = new HostsFileEditor(p); Defensive patterns
Strategy: validation
Validate before calling
// ensure the path has a file-name component
if (string.IsNullOrWhiteSpace(Path.GetFileName(path)))
path = Path.Combine(path, "hosts"); Type guard
null
Try / catch
try
{
var editor = new HostsFileEditor(path);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("file name"))
{
Log.Warn("Hosts path has no file name; using default", ex);
var editor = new HostsFileEditor(Path.Combine(DirPart, "hosts"));
} Prevention
- Never store directory-only paths for the hosts file setting.
- Normalize with Path.GetFullPath before use.
- Test the path ends with 'hosts' after configuration.
When it happens
Trigger: Constructing HostsFileEditor with a HostsFilePath that is a directory path (trailing backslash), an empty string, or otherwise lacks a file-name segment so Path.GetFileName yields null.
Common situations: Configuration storing a directory instead of the full hosts file path; string truncation when persisting the setting; tests passing directory paths.
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/dbaae5211e5ece22.
Report an issue: GitHub.
Appendix: source
Thrown at Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs:87
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);
}
}
#endregionView on GitHub (pinned to 2780d65469)