Quantcast
Viewing all articles
Browse latest Browse all 10

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)
                    txtRecords.Add(match.Groups[1].Value);
            }

            return txtRecords;
        }

Viewing all articles
Browse latest Browse all 10

Trending Articles