{"record":{"id":"dbaae5211e5ece22","repo":"BornToBeRoot/NETworkManager","slug":"hosts-file-name-is-invalid","errorCode":null,"errorMessage":"Hosts file name is invalid.","messagePattern":"Hosts file name is invalid\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs","lineNumber":87,"sourceCode":"    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    }\n\n    #endregion","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/BornToBeRoot/NETworkManager/blob/2780d65469917a296dbc15f206107d72e2d0115c/Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs#L69-L105","documentation":"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.","triggerScenarios":"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.","commonSituations":"Configuration storing a directory instead of the full hosts file path; string truncation when persisting the setting; tests passing directory paths.","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."],"exampleFix":"// before\nvar editor = new HostsFileEditor(@\"C:\\Windows\\System32\\drivers\\etc\\\");\n// after\nvar p = Path.GetFullPath(path);\nif (string.IsNullOrEmpty(Path.GetFileName(p)))\n    p = Path.Combine(p, \"hosts\");\nvar editor = new HostsFileEditor(p);","handlingStrategy":"validation","validationCode":"// ensure the path has a file-name component\nif (string.IsNullOrWhiteSpace(Path.GetFileName(path)))\n    path = Path.Combine(path, \"hosts\");","typeGuard":"null","tryCatchPattern":"try\n{\n    var editor = new HostsFileEditor(path);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"file name\"))\n{\n    Log.Warn(\"Hosts path has no file name; using default\", ex);\n    var editor = new HostsFileEditor(Path.Combine(DirPart, \"hosts\"));\n}","preventionTips":["Never store directory-only paths for the hosts file setting.","Normalize with Path.GetFullPath before use.","Test the path ends with 'hosts' after configuration."],"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"}