Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# cmd

  • 29-11-2006 8:26pm
    #1
    Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭


    Anyone know if its possible to open a command prompt screen from within a C# program and pass commands from the program to the command prompt?


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Depends on what you are trying to do...

    A hackish way (which may or may not work) would be to use a System.Diagnostics.Process and redirect the Input and Output streams. Then use the process to start "cmd" or "command" and then send your command through the redirected input. Finally read the result from the Output stream.

    i.e.
    Process p = new Process;
    p.Options.RedirectInput = true;
    p.Options.RedirectOutput = true;

    p.Start("cmd.exe");
    p.Input.WriteLine("myCommand.exe");
    string s = p.Output.Readline();
    **keep reading until no more output**.

    It's psuedo code, so that actual class will have different names for the options and whatnot :p

    Also, you can set it to not pop up the console window if you don't want it to appear.


  • Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭Ziycon


    im looking for a legit way to do it as im testing to see if i can create a cmd window then run a telnet session through it instead of using a telnet library.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Well, if you do it that way, you've just locked yourself into windows only. A slightly more direct way would be just to launch telnet directly:

    Process p = new Process();
    p.StartInfo.CommandLine = "telnet.exe";
    p.Start();

    Then just redirect the input and output and disable the creation of the console window and you're sorted. This way will work, i did it before so i could create a quick program that would ping for 2 days straight so some statistical analysis could be performed on the packetloss. (college assignment).


Advertisement