سؤال

New to programming but I was wondering if what I want to do is even possible! And if so how?

I have developed a console application which obtains the computers IP address. The program then opens the cmd prompt and runs nslookup (using said IP address) to get some information about the computer.

When the program ends I have two consoles windows open; the cmd promt console and the programs console. The cmd prompt one has the information I need. I can't figure out how to copy/ grab the information from the cmd console and put it into a string/array so I can use the information.

I have searched Google but all I keep getting is ways to copy manually from a cmd prompt window! Not how to return information from a cmd prompt window one has opened form a program!

Also please do not suggest doing a reverse DNS look up or using environment.machinename instead of using the cmd prompt. I have tried many methods and this is the only way I have been able to access the correct information which I need.

using System;
using System.Net;
using System.Net.Sockets;


namespace ProcessService
{
    static class Program
    {

        static void Main()
        {

            //The IP or Host Entry to lookup
            IPHostEntry host;
            //The IP Address String
            string localIP = "";
            //DNS lookup
            host = Dns.GetHostEntry(Dns.GetHostName());
            //Computer could have several IP addresses,iterate the collection of them to find the proper one
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }

            //Converts an IP address string to an IPAddress instance.
            IPAddress address = IPAddress.Parse(localIP);


            string strCmdText;
            strCmdText = "/k nslookup " + localIP;
            //open cmd prompt and run the command nslookup for a given IP
            System.Diagnostics.Process.Start("C:/Windows/System32/cmd.exe", strCmdText);


            //output result
            Console.WriteLine(strCmdText);
            //Wait for user to press a button to close window
            Console.WriteLine("Press any key...");
            Console.ReadLine();
        }
    }
}
هل كانت مفيدة؟

المحلول

You're starting an external process, which is good. What you need to do now is to Redirect the Standard Output. Rather than copy information from the cmd prompt window, you simply want it fed back into your program. You'll have to do a bit of parsing, but here's the sample from microsoft:

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

Source: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top