c# - Copy information from Cmd Promt Window into Console Application -
new programming wondering if want possible! , if how?
i have developed console application obtains computers ip address. program opens cmd prompt , runs nslookup (using said ip address) information computer.
when program ends have 2 consoles windows open; cmd promt console , programs console. cmd prompt 1 has information need. can't figure out how copy/ grab information cmd console , put string/array can use information.
i have searched google keep getting ways copy manually cmd prompt window! not how return information cmd prompt window 1 has opened form program!
also please not suggest doing reverse dns or using environment.machinename instead of using cmd prompt. have tried many methods , way have been able access correct information need.
using system; using system.net; using system.net.sockets; namespace processservice { static class program { static void main() { //the ip or host entry lookup iphostentry host; //the ip address string string localip = ""; //dns lookup host = dns.gethostentry(dns.gethostname()); //computer have several ip addresses,iterate collection of them find proper 1 foreach (ipaddress ip in host.addresslist) { if (ip.addressfamily == addressfamily.internetwork) { localip = ip.tostring(); } } //converts ip address string ipaddress instance. ipaddress address = ipaddress.parse(localip); string strcmdtext; strcmdtext = "/k nslookup " + localip; //open cmd prompt , run command nslookup given ip system.diagnostics.process.start("c:/windows/system32/cmd.exe", strcmdtext); //output result console.writeline(strcmdtext); //wait user press button close window console.writeline("press key..."); console.readline(); } } }
you're starting external process, good. need redirect standard output. rather copy information cmd prompt window, want fed program. you'll have bit of parsing, here's sample microsoft:
// start child process. process p = new process(); // redirect output stream of child process. p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.filename = "write500lines.exe"; p.start(); // not wait child process exit before // reading end of redirected stream. // p.waitforexit(); // read output stream first , wait. string output = p.standardoutput.readtoend(); p.waitforexit();
Comments
Post a Comment