views:

913

answers:

1

Hello,

I need to pass a simple string to a command window (running telnet), from a c# winforms app... is there a simple way to do this?

I tried the following, but it's all or nothing when redirecting standardinput--

the winforms app doesnt have to start the cmd window... the cmd window could already be running also-- I thought maybe that would be how to communicate with it--

there's 1 or 2 ?'s similar to this on so, but no one really nailed it...

p/invoking is ok also...

please help!

    Process p = new Process();
    StreamWriter sw;
    //StreamReader sr;
    //StreamReader err;

    ProcessStartInfo psi = new ProcessStartInfo(@"cmd.exe", @"/C telnet 192.168.0.10");

    private void start
    {

        psi.WindowStyle = ProcessWindowStyle.Normal;
        psi.RedirectStandardOutput = false;
        psi.RedirectStandardInput = true;

        psi.UseShellExecute = false;
        psi.CreateNoWindow = false;
        p.StartInfo = psi;


        p.Start();

    }

    private void write_to_cmd(object sender, EventArgs e)
    {

        psi.RedirectStandardInput = true;
         sw = p.StandardInput;
        ////sr = p.StandardOutput;
        ////err = p.StandardError;

        //sw.AutoFlush = true;

        if (tbComm.Text != "")
         sw.WriteLine(tbComm.Text);


        ////sw.Close();

        ////textBox1.Text = sr.ReadToEnd();
        ////textBox1.Text += err.ReadToEnd();

        //p.WaitForExit();

    }




}

}

+1  A: 

Yes, the redirection is going to be all or nothing. Do you want the user to be able to send information both programatically as well as through the command prompt? I think you want to redirect standard input to telnet, not the command prompt.

ProcessStartInfo("telnet", "192.168.0.10");

Also, you could open a telnet session by just using System.Net.Sockets instead.

OG