Results 1 to 15 of 15

Thread: How to use the command promp from C#? [resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question How to use the command promp from C#? [resolved]

    Hello!!!

    I want to execute a command from c# as if it was executed from the
    command prompt...

    how can I do that?

    thanks!!

    Last edited by dekelc; Jan 7th, 2006 at 04:25 PM.
    Dekel C.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to use the command promp from C#?

    VB Code:
    1. Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    2.         psi.RedirectStandardOutput = True
    3.         psi.RedirectStandardInput = True
    4.         psi.UseShellExecute = False
    5.         psi.CreateNoWindow = True
    6.  
    7.         Dim p As Process = Process.Start(psi)
    8.         p.StandardInput.WriteLine("ren c:\a.txt b.txt")
    9.         p.StandardInput.WriteLine("exit")

    PHP Code:
    ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
            
    psi.RedirectStandardOutput True;
            
    psi.RedirectStandardInput True;
            
    psi.UseShellExecute False;
            
    psi.CreateNoWindow True;

            
    Process p Process.Start(psi);
            
    p.StandardInput.WriteLine("ren c:\a.txt b.txt");
            
    p.StandardInput.WriteLine("exit"); 

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the command promp from C#?

    well, that's great for vb and php developers... but what about c#?
    Dekel C.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to use the command promp from C#?

    That is for C#. In PHP tags. For color formatting. Forgive my transgression.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the command promp from C#?

    do I need to add a "using" reference for ProcessStartInfo?

    because it doesn't reognise it...

    Dekel C.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to use the command promp from C#?

    Use System.Diagnostics

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the command promp from C#?

    thanks mendhak!!!!
    Dekel C.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to use the command promp from C#?

    Good. Now add [Resolved]

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How to use the command promp from C#? [resolved]

    Bear in mind that if its just a single command you can tag it onto the end of the call to start "cmd.exe" .

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the command promp from C#? [resolved]

    hey penagate! how do I do that in code?

    I mean, use the start command??

    Dekel C.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to use the command promp from C#? [resolved]

    Process.Start(appPath, argString);

    e.g.

    Process.Start("cmd", @"ren c:\a.txt b.txt");
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: How to use the command promp from C#?

    Quote Originally Posted by mendhak
    VB Code:
    1. Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    2.         psi.RedirectStandardOutput = True
    3.         psi.RedirectStandardInput = True
    4.         psi.UseShellExecute = False
    5.         psi.CreateNoWindow = True
    6.  
    7.         Dim p As Process = Process.Start(psi)
    8.         p.StandardInput.WriteLine("ren c:\a.txt b.txt")
    9.         p.StandardInput.WriteLine("exit")

    PHP Code:
    ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
            
    psi.RedirectStandardOutput True;
            
    psi.RedirectStandardInput True;
            
    psi.UseShellExecute False;
            
    psi.CreateNoWindow True;

            
    Process p Process.Start(psi);
            
    p.StandardInput.WriteLine("ren c:\a.txt b.txt");
            
    p.StandardInput.WriteLine("exit"); 


    Hello. Im trying to do the same thing but with a telnet window.
    Telnet will not allow you to redirect the standard IO

    Can you please tell me how I can send commands to the telnet without using sendkeys.send()?

    Maybe you could look at my thread
    http://www.vbforums.com/showthread.php?t=380589

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the command promp from C#? [resolved]

    well mendhak, it doesn't work after all...
    I just tried it for the first time and it faild...
    sorry...
    maybe I need to add an "Enter" key after each line?

    thanks

    Dekel C.

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to use the command promp from C#? [resolved]

    Quote Originally Posted by dekelc
    well mendhak, it doesn't work after all...
    I just tried it for the first time and it faild...
    sorry...
    maybe I need to add an "Enter" key after each line?

    thanks

    A 'writeline' includes an 'enter'. It sends the whole command.
    What failed, what did you get instead?

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the command promp from C#? [resolved]

    ok... It's working! thanks!

    but is there a way to know if my commands succeeded?

    Dekel C.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width