C# and Process class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to start a process, notepad, where I want write in some text before Notepad comes up on the screen
Obviously I don't understand how to do it
My code is
ProcessStartInfo startInfo = new ProcessStartInfo("Notepad.exe")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.UseShellExecute = false
startInfo.RedirectStandardInput = true
StreamWriter npin
Process p = new Process()
p.StartInfo = startInfo
npin = p.StandardInput
npin.WriteLine("Hej och hopp!")
p.Start()

When I run it I get the exception below! Is this a bug


An unhandled exception of type 'System.InvalidOperationException' occurred in system.dl
Additional information: StandardIn has not been redirected
 
Folke said:
I'm trying to start a process, notepad, where I want write in some text
before Notepad comes up on the screen.
Obviously I don't understand how to do it.
My code is:
ProcessStartInfo startInfo = new ProcessStartInfo("Notepad.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
StreamWriter npin;
Process p = new Process();
p.StartInfo = startInfo;
npin = p.StandardInput;
npin.WriteLine("Hej och hopp!");
p.Start();


When I run it I get the exception below! Is this a bug?



An unhandled exception of type 'System.InvalidOperationException' occurred in system.dll
Additional information: StandardIn has not been redirected.

Why don't you create a file named 'whatever.txt' and then fill with your
text - then simply call the file created from the Process class? - Does this
help?


JJ
 
Folke said:
I'm trying to start a process, notepad, where
I want write in some text before Notepad
comes up on the screen.
Obviously I don't understand how to do it.
[...]
startInfo.RedirectStandardInput = true;

You can't redirect standard input to a Windows application, only to a
console window. Perhaps you could use SendKeys instead.

P.
 
why not create the file on disk 1st then use the ProcessStartInfo
constructor that takes arguments.

ProcessStartInfo("Notepad.exe", "someFile.txt");

It's not elegant but it should do the trick.

Vince


Paul E Collins said:
Folke said:
I'm trying to start a process, notepad, where
I want write in some text before Notepad
comes up on the screen.
Obviously I don't understand how to do it.
[...]
startInfo.RedirectStandardInput = true;

You can't redirect standard input to a Windows application, only to a
console window. Perhaps you could use SendKeys instead.

P.
 
Back
Top