Please Help! Someone must have insight on this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • narpet
    New Member
    • Oct 2006
    • 69

    Please Help! Someone must have insight on this

    Hello everyone. I apologize for this reposted question, but there must be someone out there who can help me with this....

    I am using c# to build an application. One thing I would like to do is open an external app and send text to it.

    For example, I would like to open cmd.exe (already done that part with no problem) and send text to it in order to perform some function.

    So I open the command prompt, then I would like to send the text "dir" and then {enter} so that a list of the current directory is displayed.

    Any help will be greatly appreciated.

    Thanks,
    narpet
  • decyclone
    New Member
    • Dec 2006
    • 7

    #2
    Hi,

    way 1 :

    if you want to post commands to command prompt,
    a good way would be creating a batch file (.bat),
    executing that file,
    deleting it after you are done with it.

    way 2 :

    I am not an expert in win32,
    but i think you might get a handle to the command window,
    send keyboard messages from your application
    to simulate a key press on that command window.

    Hope this helps.

    Jay.

    Comment

    • narpet
      New Member
      • Oct 2006
      • 69

      #3
      Here's the code I have attached to a button. As far as I can tell from my research, this should work. But it doesn't...

      It runs the process but it doesn't enter any of the text into the window.

      Any ideas?

      Code:
                  Process p;
                  p = new Process();
                  string plogin = txtLogin.Text;
                  string ppwrd = txtPassword.Text;
                  string psite = txtSite.Text;
      
                  p.StartInfo.FileName = "cmd.exe";            
                  p.StartInfo.UseShellExecute = false;
                  p.StartInfo.RedirectStandardInput = true;
                  p.Start();
      
                  StreamWriter myStreamWriter = p.StandardInput;
                  
                  
                  myStreamWriter.WriteLine("ftp");
                  myStreamWriter.WriteLine(psite);
                  myStreamWriter.WriteLine(plogin);
                  myStreamWriter.WriteLine(ppwrd);
                  myStreamWriter.WriteLine("prompt");
                  myStreamWriter.WriteLine("lcd c:\\");
                  myStreamWriter.WriteLine("mget *");
      
                  myStreamWriter.Close();

      Comment

      Working...