Hi everyone...
Im starting c sharp scripting today and i wanted to write a script using which i can open the notepad application and write a string into it...
This is my code...

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

namespace Process_StandardInput_Sample
{
   class StandardInputTest
   {
      static void Main()
      {
		Process myProcess = new Process();

		myProcess.StartInfo.FileName = "notepad.exe";
		myProcess.StartInfo.UseShellExecute = false;
		myProcess.StartInfo.RedirectStandardInput = true;

		myProcess.Start();

		StreamWriter myStreamWriter = myProcess.StandardInput;

		String inputText;
		inputText = "hithere\n";
		Console.WriteLine(inputText);	
		myStreamWriter.WriteLine(inputText);
		myStreamWriter.Close();
		myProcess.WaitForExit();
		myProcess.Close();

      }
   }
}

Once i run the script, all i get is a notepad window and the string "hithere" is not present...what mistake am i doing here..

advance thanks for the help
DG

Use System.Windows.Forms.SendKeys.SendWait method,

Process myProcess = new Process();
        
            myProcess.StartInfo.FileName = @"notepad.exe";
            myProcess.EnableRaisingEvents = true;
            
            myProcess.Start();
            myProcess.WaitForInputIdle(1000);
            if (myProcess.Responding)
                System.Windows.Forms.SendKeys.SendWait("hithere");
             else
               myProcess.Kill();
commented: neat. I didn't know that! +6
commented: was the exact thing i was looking for.. +1

Thanks it works...
now i wanted to connect to a linux server using putty through my script

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
namespace Process_StandardInput_Sample
{
   class StandardInputTest
   {      
	   static void Main()
	   {		
		   Process myProcess = new Process();
				
					myProcess.StartInfo.FileName = @"C:\\Program Files\\PuTTY\\putty.exe";
					myProcess.EnableRaisingEvents = true;
					
					myProcess.Start();
					myProcess.WaitForInputIdle(10000);
					if (myProcess.Responding)
					{
						System.Windows.Forms.SendKeys.SendWait("192.168.6.251");/*IP address of the server*/
						System.Windows.Forms.SendKeys.SendWait("{TAB}");/*To select the type of connection*/
						System.Windows.Forms.SendKeys.SendWait("{TAB}");
						System.Windows.Forms.SendKeys.SendWait("{LEFT}");
						System.Windows.Forms.SendKeys.SendWait("{LEFT}");/*To select telnet*/
						System.Windows.Forms.SendKeys.SendWait("{ENTER}");
						myProcess.WaitForInputIdle(10000);
						System.Windows.Forms.SendKeys.SendWait("username");
						System.Windows.Forms.SendKeys.SendWait("{ENTER}");
						myProcess.WaitForInputIdle(10000);
						System.Windows.Forms.SendKeys.SendWait("password");
						System.Windows.Forms.SendKeys.SendWait("{ENTER}");
					}
					 else
					   myProcess.Kill();
	   }
   }
}

But each time i run the script i get diffenrent results...i feel the the script is not given the application enough time to respond...is there any way i can read wat ever is being displayed on my telnet window...so that i can parse for the word "login" and only then send the username and password....
thanks a lot...

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace Process_StandardInput_Sample
{
	class StandardInputTest
	{      
	   static void Main()
	   {		
			Process myProcess = new Process();
			myProcess.StartInfo.FileName = @"C:\\Program Files\\PuTTY\\putty.exe";
			
			myProcess.Start();
			Thread.Sleep(5000);
			if (myProcess.Responding)
			{
				System.Windows.Forms.SendKeys.SendWait("192.168.6.251");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{TAB}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{TAB}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{LEFT}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{LEFT}");						
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("deepak");
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("deepak");
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("ls");
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
			}
			 else
			   myProcess.Kill();
	   }
   }
}

This is able to connect to a remote linux server and send commands to the session thats opened.

Now i wanna try to redirect the output thats getting displayed on the putty session to a file....

i was trying out the StreamReader and StreamWriter but i was only able to log wat ever commands im entrin and not wats getting displayed...

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace Process_StandardInput_Sample
{
	class FileClass
	{
		StreamWriter SW;
		
		public FileClass()
		{
			SW = File.CreateText("c:\\MyTextFile.txt");
			Console.WriteLine("File Created SuccessFully");
		}
		public void WriteToFile(string line)
		{
			SW.WriteLine(line);				
		}
		
		~FileClass()
		{
			//SW.Close();		
		}
	}

	class StandardInputTest
	{      
	   static void Main()
	   {		
			Process myProcess = new Process();
			FileClass f = new FileClass();
			myProcess.StartInfo.FileName = @"C:\\Program Files\\PuTTY\\putty.exe";
			myProcess.EnableRaisingEvents = true;
			myProcess.StartInfo.UseShellExecute = false;
			myProcess.StartInfo.RedirectStandardOutput = true;
			myProcess.StartInfo.RedirectStandardInput = true;
			
			myProcess.Start();
			f.WriteToFile("date");
			StreamReader myStreamReader = myProcess.StandardOutput;
			StreamWriter myStreamWriter = myProcess.StandardInput;
			Thread.Sleep(5000);
			if (myProcess.Responding)
			{
				System.Windows.Forms.SendKeys.SendWait("192.168.6.251");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{TAB}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{TAB}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{LEFT}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{LEFT}");						
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("deepak");
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("deepak");
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				Thread.Sleep(500);
				System.Windows.Forms.SendKeys.SendWait("ls");
				System.Windows.Forms.SendKeys.SendWait("{ENTER}");
				string outfile = myStreamReader.ReadLine();
				f.WriteToFile(outfile);
			}
			 else
			   myProcess.Kill();
	   }
   }
}

Is this possible..?
or is there a better way to achieve this....?

Why don't you support telnet or SSH communication natively instead of using sendkeys automation?

C# Telnet Client

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.