Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 27th, 2008, 01:05 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default running a batch file from cmd from C#

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...01/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.


 
Old March 27th, 2008, 05:53 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

a) I think you need to split the command into program and arguments, and
b) You need to supply the /c switch to the cmd.exe program to make it run a batch file.

Code:
System.Diagnostics.Process.Start("cmd.exe", @"/c C:\batchfile.bat")
/- Sam Judson : Wrox Technical Editor -/
 
Old March 29th, 2008, 07:56 AM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thanks

http://www.youtubeline.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
My batch file works, running it from VB does not Kal2219 Visual Basic 2005 Basics 3 February 26th, 2008 02:35 PM
Add Date to File Using Cmd ecom Other Programming Languages 1 November 13th, 2007 05:16 PM
Need Help with DOS batch file .. Arul20 Forum and Wrox.com Feedback 5 August 31st, 2007 06:51 AM
Batch File Allan320 Access 10 June 8th, 2006 06:46 AM
Running cmd line .EXE through PHP geolast PHP How-To 2 January 6th, 2005 02:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.