{"record":{"id":"06669b345932a8cf","repo":"TechnitiumSoftware/DnsServer","slug":"dhcpserver-scope-file-format-is-invalid","errorCode":null,"errorMessage":"DhcpServer scope file format is invalid.","messagePattern":"DhcpServer scope file format is invalid\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dhcp/Scope.cs","lineNumber":127,"sourceCode":"        #region constructor\n\n        public Scope(string name, bool enabled, IPAddress startingAddress, IPAddress endingAddress, IPAddress subnetMask, LogManager log, DhcpServer dhcpServer)\n        {\n            ValidateScopeName(name);\n\n            _name = name;\n            _enabled = enabled;\n\n            ChangeNetwork(startingAddress, endingAddress, subnetMask);\n\n            _log = log;\n            _dhcpServer = dhcpServer;\n        }\n\n        public Scope(Stream s, LogManager log, DhcpServer dhcpServer)\n        {\n            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != \"SC\")\n                throw new InvalidDataException(\"DhcpServer scope file format is invalid.\");\n\n            _log = log;\n            _dhcpServer = dhcpServer;\n\n            BinaryReader bR = new BinaryReader(s);\n\n            byte version = bR.ReadByte();\n            switch (version)\n            {\n                case 1:\n                case 2:\n                case 3:\n                case 4:\n                case 5:\n                case 6:\n                case 7:\n                case 8:\n                case 9:","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dhcp/Scope.cs#L109-L145","documentation":"Scope(Stream) constructor checks the file magic header: the first two ASCII bytes must be 'SC'. Anything else means the stream is not a valid DhcpServer scope file, and it throws InvalidDataException.","triggerScenarios":"new Scope(stream, log, dhcpServer) where Encoding.ASCII.GetString(stream.ReadExactly(2)) != 'SC' (empty, partial, wrong-type, or text file).","commonSituations":"Pointing the scopes directory at a non-scope file. Truncated/corrupt scope file. File overwritten by another tool. Wrong working directory.","solutions":["Remove the offending file from the scopes directory so the server stops loading it.","Restore the scope file from backup.","Recreate the scope via the UI/API if no backup exists."],"exampleFix":"// operational, not code\n// before: scopes/lan.txt is a text note -> 'SC' magic mismatch\n// after: move the file out of the scopes dir: mv scopes/lan.txt notes/lan.txt","handlingStrategy":"try-catch","validationCode":"byte[] head = new byte[2];\nusing (var fs = File.OpenRead(path)) { if (fs.Read(head, 0, 2) != 2 || head[0] != (byte)'S' || head[1] != (byte)'C') throw new InvalidDataException($\"{path} is not a valid scope file\"); }","typeGuard":"static bool IsScopeFile(string path) { try { using var fs = File.OpenRead(path); var b = new byte[2]; return fs.Read(b, 0, 2) == 2 && b[0] == (byte)'S' && b[1] == (byte)'C'; } catch { return false; } }","tryCatchPattern":"try { var scope = new Scope(stream, log, dhcpServer); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"scope file format is invalid\"))\n{ /* skip/restore the corrupt file */ }","preventionTips":["Keep non-scope files out of the scopes directory.","Validate the 'SC' magic before loading.","Back up and verify scope files."],"tags":["dhcp","scope","file-format","serialization","corruption"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}