从 C# 运行 .bat 文件
我面临一个奇怪的问题,我有一个 .bat 文件,其中包含用于重命名文件的代码,当我手动打开 .bat 文件时,它会执行上面写的操作,即重命名文件,但是当我尝试打开时它以 C# 方式编程,它不执行任何操作,它只是打开文件并且不编译它写入的内容。我输入了该代码:
Process.Start(@"file.bat");
我也知道如果您在cmd中输入路径并按回车键,它将打开文件并编译它,所以我写了:
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()
但仍然是同样的问题:文件正在打开,但从未执行其写入的操作进入其中。
编辑:[我想出了为什么]
我拍摄了应该运行 .bat 文件的 CMD 窗口的快照,但我得到了错误:
ERROR : THE FILE SPECIFIED COULD NOT BE FOUND
但是怎么办?当我手动运行 .bat 文件时,它工作正常!
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");
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]
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
but how ? when i run the .bat file manually it works FINE!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样尝试。 ProcessStartInfo
you may try like this. ProcessStartInfo
您可能需要额外的管理员权限才能执行批处理文件中的预期内容。如果是这样,请使用 Process.Start() 的替代版本:
You may be requiring additional admin previleges to execute what is intended in the Batch file. If so, use the alternative version of Process.Start():
将批处理文件的第一行设置为:
Pushd %~dp1
这将设置批处理文件的工作目录。
马丁
Make the first line of the batch file this:
pushd %~dp1
This will set the batch files working directory.
martyn
这应该对您有用:
myBatchFileName
是 .bat 文件的实际名称......This should work for you:
myBatchFileName
is the actual name of .bat file.....