C# and Process class

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
 
J

James

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
 
P

Paul E Collins

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.
 
V

Vince Apesa

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top