Simple Command Line Execution. Created : Sunday, Sept 23rd 2001. Written By : Trevor Pinkney. Version : Dot Net Beta 2 C# .Net lesson that Demonstrates how to execute a batch file */ using System; namespace Learn { class cmdShell { [STAThread] // Lets main know that multiple threads are involved. static void Main(string[] args) { System.Diagnostics.Process proc; // Declare New Process proc = System.Diagnostics.Process.Start("C:\\test.bat"); // run test.bat from command line. proc.WaitForExit(); // Waits for the process to end. } } } |
Intermediate Command Line Execution. Created : Sunday, Sept 23rd 2001. Written By : Trevor Pinkney. Version : Dot Net Beta 2 C# .Net lesson that Demonstrates how to execute a program from the command line. It will open a specified file in window's notepad within a maximized window. The program will stop when the user closes the window. This program could be easily modified to run powerful batch files that could link resource files - Or a crazy program to annoy your friends that opens 1000 instances of notepad. */ using System; namespace Learn { class cmdShell { [STAThread] // Lets main know that multiple threads are involved. static void Main(string[] args) { Console.WriteLine("Hello, I'm creating a process"); System.Diagnostics.Process proc; // Declare New Process System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo(); // Declare New Process Starting Information procInfo.UseShellExecute = true; //If this is false, only .exe's can be run. procInfo.WorkingDirectory = "C:"; //execute notepad from the C: Drive procInfo.FileName = "notepad.exe"; // Program or Command to Execute. procInfo.Arguments = "C:\\boot.ini"; //Command line arguments. procInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; // Will run notepad maximized, the process could also be (ProcessWindowStyle.Hidden) or (ProcessWindowStyle.Minimized) Console.WriteLine("Notepad Process Started at : {0}", DateTime.Now.ToString()); proc = System.Diagnostics.Process.Start(procInfo); // same as typing "notepad.exe C:\boot.ini" from windows Start->Run. proc.WaitForExit(); // Waits for the process to end. (ie. when user closes it down) Console.WriteLine("Notepad Process Closed at : {0}", DateTime.Now.ToString()); Console.WriteLine("\n\nPress Enter To Continue."); Console.ReadLine(); if(!proc.HasExited) // Just To Be Safe. { proc.Kill();} } } } |