×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

nslookup output with C#

nslookup output with C#

nslookup output with C#

(OP)
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();
        }  
               }
            }

RE: nslookup output with C#

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";
            /// here is the key code (these two lines)
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;

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

            System.Text.StringBuilder sb = new StringBuilder();
            while (output.Peek() > -1)
            {
                /// foreach outputed line, store it in the StringBuilder and append a new line after it
                sb.Append(output.ReadLine() + Environment.NewLine);
            }
            txtNSLookup.Text = sb.ToString();
            psi = null; p = null;
        }

RE: nslookup output with C#

Actually on second thought, just do this
Instead of

CODE

System.Text.StringBuilder sb = new StringBuilder();
while (output.Peek() > -1)
{
     /// foreach outputed line, store it in the StringBuilder and append a new line after it
     sb.Append(output.ReadLine() + Environment.NewLine);
}
txtNSLookup.Text = sb.ToString();

Simplify it with this

CODE

  
/// Much Cleaner
txtNSLookup.Text = output.ReadToEnd().ToString();

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close