TechnitiumSoftware/DnsServer · error · InvalidDataException

DhcpServer scope file format is invalid.

Error message

DhcpServer scope file format is invalid.

What it means

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.

Source

Thrown at DnsServerCore/Dhcp/Scope.cs:127

        #region constructor

        public Scope(string name, bool enabled, IPAddress startingAddress, IPAddress endingAddress, IPAddress subnetMask, LogManager log, DhcpServer dhcpServer)
        {
            ValidateScopeName(name);

            _name = name;
            _enabled = enabled;

            ChangeNetwork(startingAddress, endingAddress, subnetMask);

            _log = log;
            _dhcpServer = dhcpServer;
        }

        public Scope(Stream s, LogManager log, DhcpServer dhcpServer)
        {
            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != "SC")
                throw new InvalidDataException("DhcpServer scope file format is invalid.");

            _log = log;
            _dhcpServer = dhcpServer;

            BinaryReader bR = new BinaryReader(s);

            byte version = bR.ReadByte();
            switch (version)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Remove the offending file from the scopes directory so the server stops loading it.
  2. Restore the scope file from backup.
  3. Recreate the scope via the UI/API if no backup exists.

Example fix

// operational, not code
// before: scopes/lan.txt is a text note -> 'SC' magic mismatch
// after: move the file out of the scopes dir: mv scopes/lan.txt notes/lan.txt
Defensive patterns

Strategy: try-catch

Validate before calling

byte[] head = new byte[2];
using (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"); }

Type guard

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; } }

Try / catch

try { var scope = new Scope(stream, log, dhcpServer); }
catch (InvalidDataException ex) when (ex.Message.Contains("scope file format is invalid"))
{ /* skip/restore the corrupt file */ }

Prevention

When it happens

Trigger: new Scope(stream, log, dhcpServer) where Encoding.ASCII.GetString(stream.ReadExactly(2)) != 'SC' (empty, partial, wrong-type, or text file).

Common situations: Pointing the scopes directory at a non-scope file. Truncated/corrupt scope file. File overwritten by another tool. Wrong working directory.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/06669b345932a8cf. Report an issue: GitHub.