Thread: Command Prompt use within C# class file

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    Command Prompt use within C# class file

    I am making a console application whose sole user interaction is form based. Lets say there is a button on my main form which, when clicked, must run another program via command prompt. I've been trying to find a way to do the following steps in a hardcoded manor:
    1) open a command prompt
    2) change the directory to the folder path the other program lies in
    3) run said program

    All of these would preferably be done without the command prompt being visible to the user. So far I've achieved step 1, which seemed the easy part. I can't however figure out how to input commands to the prompt, which is what is needed to do the next two steps.

    I've been opening command prompt by writing
    ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
    and then
    Process.Start(startInfo);
    after I changed aspects like startInfo.CreateNoWindow

    I'd very much appreciate some direction here. Also, please let me know if you'd like additional information.
    Last edited by cStudy; 09-27-2010 at 11:02 AM. Reason: Additional Information

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Would System.Diagnostics.Process.Start not be suitable for this?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    What is the program you are trying to run? Why must it be run from the command prompt? Why not run the program directly from Process.Start?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Good questions, and ones I should have thought to state. I'm running a perl script, which the only way I was aquanted with running was to type "perl processName.pl" in command prompt. The problem I had with Process.Start was that, while that does indeed open command prompt, it doesn't allow me to write lines to the prompt. I could be wrong here; it may be possible, but I couldn't figure or find out how.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Try this...

    Code:
    String path = "c:\\whatever the folder path"; // set the path folder containing perl.exe and the .pl files
    Process.Start(path + "\\perl.exe", path + "\\processName.pl");

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    That didn't work for me. It returned an error along the lines of path not found. I know however that the path does exist because the code below is a way a friend showed me just recently which I've implemented and which does work. Assume for this that processPath is a string which contains the process path.

    This is a more generic version of the code I'm using in case another has the same issue as me. I hope this helps.

    Code:
                string returnvalue = "";
    
                // Starts the new process as command prompt
                ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
                info.UseShellExecute = false;
                info.RedirectStandardInput = true;
                info.RedirectStandardOutput = true;
                // Makes it so the command prompt window does appear
                info.CreateNoWindow = true;
    
                using (Process process = Process.Start(info))
                {
                    StreamWriter sw = process.StandardInput;
                    StreamReader sr = process.StandardOutput;
    
                    // This for loop could be used if you had a string[] commands where each string in commands 
                    // is it's own command to write to the prompt. I chose to hardcode mine in.
                    //foreach (string command in commands)
                    //{
                    //    sw.WriteLine(command);
                    //}
                    sw.WriteLine("cd " + processPath);
                    sw.WriteLine("perl process.pl");
    
                    sw.Close();
                    returnvalue = sr.ReadToEnd();
                }
    
                return returnvalue;

  7. #7
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107

    Not a C# expert yet, but...

    cStudy,

    Rather than:

    Quote Originally Posted by theoobe View Post
    Try this...

    Code:
    String path = "c:\\whatever the folder path"; // set the path folder containing perl.exe and the .pl files
    Process.Start(path + "\\perl.exe", path + "\\processName.pl");
    May I suggest:

    Code:
    String path = "c:\\path to perl files"; // set the path folder  containing the .pl files
    // ...
    sw.WriteLine( "cd" + path );
    sw.WriteLine( "perl process.pl" );
    // ...
    You also used a ProcessStartInfo object, which looks nifty by the way, what did you give it as a path (to search for executables I mean)? Does the path to the perl executable also need to be given during the second WriteLine?

    Best Regards,

    New Ink -- Henry
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Thanks Henry. What you see in the code posted is pretty much the function I have. As such, there is no path specified to search for executables. It sounded like you were asking in regards to the command executable. The machine just recognizes that. As for "perl", this also needs no path. The directory the perl script is in is "processPath", which is why I change the directory to there. So pretty much what I've written should work for others, assuming they have perl, a process, and a path to that process figured out.

    Thanks Henry and theoobe.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    cStudy,

    I'm blushing over it cStudy. I've been watching the post ardently, and I was wondering about it. I just installed a C# compiler recently, and I've been drooling to find examples for testing. It seems that the market really likes C#, and getting some work together is always a goal of mine. Thank you for something with which to play around....

    Now I'll take advantage of this new shiny compiler to test out. It sounds like this was translated, so I'll stop costing you money as soon as possible. What (spoken) language should I get to work on in order to avoid the friction of translation? My Spanish is about ten years old, but I really like Telemundo. My first language was German (and I'm still good), but I've studied Russian, Chinese, Arabic and French enough to say hello and that I don't speak the language very well.

    Best Regards,

    New Ink -- Henry
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    No problem Henry. I'm glad I could give you a new toy to play with. I'm very impressed with how many languages you've studied. I've never been much for learning other languages, though I truthfully haven't tried very much. My first and only fluet language is actually English, so feel free to talk away without working further on any other language. Out of curiousity, what about my speech makes it sound translated?

    PS: Since you like Telemundo and at one point studied Spanish I thought you might like this:
    YouTube - Que Hora es ? Soap Op'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM