Skip to content

Instantly share code, notes, and snippets.

@ugnb
Created November 12, 2018 13:50
Show Gist options
  • Save ugnb/593de7bff11ee27238e7f61b8930477a to your computer and use it in GitHub Desktop.
Save ugnb/593de7bff11ee27238e7f61b8930477a to your computer and use it in GitHub Desktop.
Execute external program (nslookup) in c# asynchronously to resolve domain MX records.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace DNSResolve
{
class Program
{
public static async Task Main(string[] args)
{
var lookupResponse = await RunProcessAsync("google.com");
Console.WriteLine(lookupResponse);
}
private static async Task<string> RunProcessAsync(string domain)
{
var tcs = new TaskCompletionSource<string>();
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "nslookup",
Arguments = $"-q=mx {domain}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
proc.Exited += (sender, args) =>
{
tcs.SetResult(proc.StandardOutput.ReadToEnd());
proc.Dispose();
};
proc.Start();
return await tcs.Task;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment