CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2005
    Posts
    180

    Running a Batch File from a Command Window Launched from a C# Program

    I have been able to launch a batch file from a C# program in a number of ways.

    Duncan Mackenzie's blog on msdn shows a number of ways how it can be done and I have tried them. (http://blogs.msdn.com/csharpfaq/arch...1/146375.aspx).

    But I have a unique problem. The batch file launches a program that seems to require that a command window (cmd) be open when it runs. Because of this, the batch file works if I launch it from a command line, but when I launch it in a C# program, an exception is thrown.

    So my question is this. How can I open a cmd window from C# and feed it the command to run the batch routine?

    There seems to be some sort of special block or prehibitation on running "CMD.EXE" from C#. I have tried a number of tricks. I have tried System.Diagnostics.Process.Start(@"cmd C:\bacthfile.bat"); but that did not work. I even tried the "start" command line argument which is supposed to launch a second command window. I did this by putting this command in one batch file to launch a second window. But both windows did not open when I did this from the C# program.

  2. #2
    Join Date
    Jun 2007
    Posts
    16

    Re: Running a Batch File from a Command Window Launched from a C# Program

    You should make a new process
    System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

    then define the process startinfo

    myProcess.StartInfo.Argument = "whateverbatch.bat"

    Its not that exactly, but I don't have VS installed on my new box. So, give that a shot. Oh yeah, and naturally you have to define any startinfo prior to starting.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Running a Batch File from a Command Window Launched from a C# Program

    Here's some vb6 code. the /c closes the window when the app finishes

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Shell ("C:\Windows\System32\cmd.exe /c  " & _
         "C:\Windows\System32\ipconfig >> d:\myip.txt"), vbNormalFocus
    End Sub
    The results are appended to a file on a different drive.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Running a Batch File from a Command Window Launched from a C# Program

    Quote Originally Posted by Complete
    But I have a unique problem. The batch file launches a program that seems to require that a command window (cmd) be open when it runs. Because of this, the batch file works if I launch it from a command line, but when I launch it in a C# program, an exception is thrown.

    So my question is this. How can I open a cmd window from C# and feed it the command to run the batch routine?

    There seems to be some sort of special block or prehibitation on running "CMD.EXE" from C#. I have tried a number of tricks. I have tried System.Diagnostics.Process.Start(@"cmd C:\bacthfile.bat"); but that did not work. I even tried the "start" command line argument which is supposed to launch a second command window. I did this by putting this command in one batch file to launch a second window. But both windows did not open when I did this from the C# program.
    What is the exception? Wrap a try/catch( Exception e ) block around the Process Startup code. It may be something simple such as not finding cmd.exe.

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

Featured