2012-08-11 174 views
0

我有一个小问题。好吧,让我们从我的C#控制台应用程序说我想运行一个批处理文件,将采取一个参数。我的C#应用​​程序停止处的字符串变量将是传递给批处理文件的字符串参数。我该怎么去做呢?从C#执行批处理文件#

这是到目前为止我的代码我的C#控制台程序:

//String argument to pass to the batch file 
string message = "Hello World"; 

System.Diagnostics.Process process = new System.Diagnostics.Process(); 

//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
startInfo.FileName = "Greetings.bat"; 
startInfo.Arguments = "/C " + message; 
process.StartInfo = startInfo; 
process.Start(); 

我的批处理文件

CLS 
@ECHO OFF 
ECHO %1   
+1

可能重复(http://stackoverflow.com/questions/361097/c-sharp-service-cannot-execute-batch-file) – Bernard 2012-08-11 13:49:01

+1

或http:/ /stackoverflow.com/questions/7101326/running-a-batch-file-from-c-sharp – 2012-08-11 13:56:01

回答

0

你可以给这样的说法。

ProcessStartInfo psi = new ProcessStartInfo(filePath); 
psi.WindowStyle = ProcessWindowStyle.Hidden; 
psi.Arguments = "value1"; 
的[C#服务不能执行批处理文件?]
+0

请注意,使用此代码,您提供了一个参数'value1value2' – 2012-08-11 13:54:02