从C#运行.bat文件 [英] Run .bat file from C#
问题描述
我面临一个奇怪的问题,我有一个.bat文件,其中包含一个重命名文件的代码,当我手动打开.bat文件时,它会执行它所写的重命名文件,但是当我尝试从C#以程序方式打开它,它没有做任何事情,它jsut打开文件并且不编译它写入的内容。我键入了该代码:
i am facing a strange issue , i have a .bat file which contains a code for renaming a file , when i open the .bat file manually it does what it is written on it which is renaming a file , but when i try to open it programtically from C# , it doesnt do anything , it jsut open the file and do not compile what it is written into . i typed that code :
Process.Start(@"file.bat");
我也知道如果你输入cmd的路径并按下输入它将打开文件并编译它,所以我写道:
I also knew if you typed the path into cmd and pressed enter it will open the file and compile it , so i wrote that :
ProcessStartInfo psi3 = new ProcessStartInfo("cmd", "/c " + '"'+"D:\\my Work\\My Soft\\CA Delete\\CA Delete\\bin\\Debug\\file.bat"+'"');
Process p3 = Process.Start(psi3);
p3.WaitForExit()
但问题仍然存在:文件正在打开但是永远都不会写入它。
But still the same issue : the file is being opened but never does what it is written into it.
编辑:[我想为什么]
EDIT : [I Figured why]
我拍了一下应该运行.bat文件的CMD窗口的镜头,我得到了错误:
I took a snap shot of the CMD windows that should run the .bat file and i got ERROR :
ERROR : THE FILE SPECIFIED COULD NOT BE FOUND
但是怎么样?当我手动运行.bat文件时,它可以正常运行!!!
but how ? when i run the .bat file manually it works FINE!!!
推荐答案
这应该适合你:
String myBatchFileName = "name_of_file.bat";
System.Diagnostics.Process.Start(myBatchFileName);
- 注意变量
myBatchFileName
是.bat文件的实际名称..... - Note that variable
myBatchFileName
is the actual name of .bat file.....
这篇关于从C#运行.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!