0

I am trying to open a Notepad process and write strings into it after initialization. This is just a "POC" for my real goal which is to start a process, a user terminal of some sort, and completely control it through my app.

I searched the question here in different forms and found this link which was exactly what I was searching for!

Unfortunately, it doesn't work :/

This is the simple code:

static void Main(string[] args)
    {
        ProcessStartInfo ProcessInfo = new ProcessStartInfo("notepad");
        ProcessInfo.RedirectStandardInput = true;
        ProcessInfo.RedirectStandardOutput = true;
        ProcessInfo.RedirectStandardError = true;
        ProcessInfo.UseShellExecute = false;
        ProcessInfo.ErrorDialog = false;

        Process aProcess = new Process();
        aProcess.StartInfo = ProcessInfo;
        aProcess.Start();

        StreamWriter processWriter = aProcess.StandardInput;
        StreamReader processReader = aProcess.StandardOutput;
        StreamReader processError = aProcess.StandardError;

        while (!aProcess.Responding)
        {
            System.Threading.Thread.Sleep(5000);
        }
        processWriter.WriteLine("OMG IT FINALLY WORKED");
        aProcess.WaitForExit();

    }

The notepad process opens up but the information I tried to write with my processWriter is not present.

Does anyone have any idea why it's not working and how to make it work without walkarounds like using keystrokes and stuff?

Thanks in advance!

Oldbob
  • 1
  • 1
  • 1
    `Process.StandardInput` is for console applications, not Windows graphical applications such as Notepad. You need to use `SendKeys` for the latter; see http://stackoverflow.com/a/15292428/1149773 – Douglas Aug 01 '15 at 10:09
  • First of all, thank you for the quick response! Secondly, what exactly is the difference? Why isn't it possible to direct data to this kind of process's stdin? I assumed they were all the same :X – Oldbob Aug 01 '15 at 10:51
  • Graphical applications typically offer richer functionality than can be controlled by a text stream. Word, for example, supports formatting and diagramming; you cannot control these by sending strings to its stdin. In case of Notepad, I suppose it could have been made to process stdin input, since it's plain-text anyway; but it's not the convention to do so. – Douglas Aug 01 '15 at 11:33

0 Answers0