Getting DNS TXT Record using C#

In this example we will get the DNS TXT Record for a given hostname using C# to run nslookup at the command line.

private static IList GetTxtRecords(string hostname)
            IList txtRecords = new List();
            string output;
            string pattern = string.Format(@"{0}\s*text =\s*""([\w\-\=]*)""", hostname);

            var startInfo = new ProcessStartInfo("nslookup");
            startInfo.Arguments = string.Format("-type=TXT {0}", hostname);
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            using (var cmd = Process.Start(startInfo))
                output = cmd.StandardOutput.ReadToEnd();
            MatchCollection matches = Regex.Matches(output, pattern, RegexOptions.IgnoreCase);
            foreach (Match match in matches)
                if (match.Success)

            return txtRecords;


No comments yet.

Add Yours

  • Author Avatar


Comment Arrow

About Author


Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.