Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nslookup output with C#

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
0
0
US
i am new to C# and i am having some issues. I have a button, 2 texstboxes (1 for hostname, other for results) I can get the below code to work and output to the Console, but I cannot get it to output to the txtnslookup textbox. No matter what I change. I'm a VB programmer, but want to start on C#. help..

Code:
private void btnNSLookup_Click(object sender, EventArgs e)
        {
            // Do a few lookups by host name and address  
            DNSLookup(txthostname.Text);
            
              
        }
        protected static void DNSLookup(string hostNameOrAddress)
        {
            Console.WriteLine("Lookup: {0}\n", hostNameOrAddress);

            IPHostEntry hostEntry = Dns.GetHostEntry(hostNameOrAddress);
            Console.WriteLine("  Host Name: {0}", hostEntry.HostName);

            IPAddress[] ips = hostEntry.AddressList;
            foreach (IPAddress ip in ips)
            {
               Console.WriteLine("  Address: {0}", ip);
                
                
            }

            Console.WriteLine();
        }  
               }
            }
 
You can do something like this:
Code:
private void getNSLookup(string IPAddress)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
            psi.FileName = "nslookup.exe";
            psi.Arguments = "127.0.0.1";
            [green]/// here is the key code (these two lines)[/green]
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;

            psi.CreateNoWindow = false;
            p.StartInfo = psi;
            p.Start();
            
            p.WaitForExit();
            [green]/// this is where the output from nslookup will be stored, p.StandardOutput[/green]
            System.IO.StreamReader output = p.StandardOutput;

            System.Text.StringBuilder sb = new StringBuilder();
            while (output.Peek() > -1)
            {
                [green]/// foreach outputed line, store it in the StringBuilder and append a new line after it[/green]
                sb.Append(output.ReadLine() + Environment.NewLine);
            }
            txtNSLookup.Text = sb.ToString();
            psi = null; p = null;
        }
 
Actually on second thought, just do this
Instead of
Code:
System.Text.StringBuilder sb = new StringBuilder();
while (output.Peek() > -1)
{
     [green]/// foreach outputed line, store it in the StringBuilder and append a new line after it[/green]
     sb.Append(output.ReadLine() + Environment.NewLine);
}
txtNSLookup.Text = sb.ToString();

Simplify it with this

Code:
[green]/// Much Cleaner[/green]
txtNSLookup.Text = output.ReadToEnd().ToString();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top