Begineers How to: Creating a new instance of explorer / any command !!!

Jan 6 2004 10:57 AM
I posted this the other day and saw there where alot of views and since I now have an extremly easy solution I thought I would post it !!! I believe the original was from http://www.aspemporium.com/aspEmporium/cshrp/howtos/howto.asp?hid=18 but I'm not sure (it was very late and I did not keep track of where I pulled it from.) ... So if this looks like yours Thanks alot for the help..... :-) for your form: public class Form1 : System.Windows.Forms.Form { public Cmd.CmdClass COMMAND; after InitializeComponent(); //create new instance of CmdClass COMMAND = new Cmd.CmdClass(); Ex: private void button1_Click(object sender, System.EventArgs e) { // textBox1.text = "Explorer.exe" // or // ex: @"ipconfig /all > c:\iptext.txt" COMMAND.CmdRun(textBox1.Text); } For the Cmd Class using System; using System.Diagnostics; using System.IO; namespace Cmd { public class CmdClass { public string command; public void CmdRun(string command) { ProcessStartInfo startinfo; Process process = null; OperatingSystem os; string stdoutline ; StreamReader stdoutreader; try { //check windows version os = Environment.OSVersion; if (os.Platform != PlatformID.Win32NT) { throw new PlatformNotSupportedException( "Supported on Windows NT or later only" ); } os = null; //check arguments if (command == null || command.Trim().Length == 0) { throw new ArgumentNullException( "command", "the command cannot be null" ); } startinfo = new ProcessStartInfo(); // use command prompt startinfo.FileName = "cmd.exe"; // /c switch sends a command startinfo.Arguments = "/C " + command; // don't exec with shellexecute api startinfo.UseShellExecute = false; // redirect stdout to this program startinfo.RedirectStandardOutput = true; // don't open a cmd prompt window startinfo.CreateNoWindow = true; // start cmd prompt, execute command process = Process.Start(startinfo); // retrieve stdout line by line stdoutreader = process.StandardOutput; while((stdoutline=stdoutreader.ReadLine())!= null) { Console.WriteLine(stdoutline); } stdoutreader.Close(); stdoutreader = null; } catch { throw; } finally { if (process != null) { // close process handle process.Close(); } //cleanup process = null; startinfo = null; } } } } I hope this helps sum of the other newbies, or anyone for that matter, as it has helped me !!! PS : If this is the wrong place to post this please let me know !!