{"record":{"id":"3229268071432c95","repo":"BornToBeRoot/NETworkManager","slug":"hosts-file-path-is-invalid","errorCode":null,"errorMessage":"Hosts file path is invalid.","messagePattern":"Hosts file path is invalid\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs","lineNumber":85,"sourceCode":"    /// Regex to match a hosts file entry with optional comments, supporting IPv4, IPv6, and hostnames\n    /// </summary>\n    private static readonly Regex HostsFileEntryRegex = new(RegexHelper.HostsEntryRegex);\n\n    #endregion\n\n    #region Constructor\n\n    static HostsFileEditor()\n    {\n        // Create a file system watcher to monitor changes to the hosts file\n        try\n        {\n            Log.Debug(\"HostsFileEditor - Creating file system watcher for hosts file...\");\n\n            // Create the file system watcher\n            HostsFileWatcher = new FileSystemWatcher();\n            HostsFileWatcher.Path = Path.GetDirectoryName(HostsFilePath) ??\n                                    throw new InvalidOperationException(\"Hosts file path is invalid.\");\n            HostsFileWatcher.Filter = Path.GetFileName(HostsFilePath) ??\n                                      throw new InvalidOperationException(\"Hosts file name is invalid.\");\n            HostsFileWatcher.NotifyFilter = NotifyFilters.LastWrite;\n\n            // Maybe fired twice. This is a known bug/feature.\n            // See: https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice\n            HostsFileWatcher.Changed += (_, _) => OnHostsFileChanged();\n\n            // Enable the file system watcher\n            HostsFileWatcher.EnableRaisingEvents = true;\n\n            Log.Debug(\"HostsFileEditor - File system watcher for hosts file created.\");\n        }\n        catch (Exception ex)\n        {\n            Log.Error(\"Failed to create file system watcher for hosts file.\", ex);\n        }\n    }","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/BornToBeRoot/NETworkManager/blob/2780d65469917a296dbc15f206107d72e2d0115c/Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs#L67-L103","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar editor = new HostsFileEditor(\"hosts\");\n// after\nvar hostsPath = Path.IsPathRooted(path) ? path : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @\"drivers\\etc\\hosts\");\nvar editor = new HostsFileEditor(hostsPath);","handlingStrategy":"validation","validationCode":"// before constructing HostsFileEditor\nif (string.IsNullOrWhiteSpace(path) || !Path.IsPathRooted(path))\n    path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @\"drivers\\etc\\hosts\");\nif (string.IsNullOrEmpty(Path.GetDirectoryName(path)))\n    throw new ArgumentException(\"Hosts path must include a directory\", nameof(path));","typeGuard":"null","tryCatchPattern":"try\n{\n    var editor = new HostsFileEditor(path);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Hosts file\"))\n{\n    Log.Warn(\"Invalid hosts path configured, falling back to system default\", ex);\n    var editor = new HostsFileEditor(DefaultHostsPath);\n}","preventionTips":["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."],"tags":["filesystem","path","hosts-file"],"backgroundTag":"invalid-config-value","analyzedSha":"2780d65469917a296dbc15f206107d72e2d0115c","analyzedAt":"2026-09-12T17:00:17.986Z","contentChangedAt":"2026-09-12T17:00:17.986Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}