从 C# 运行 .bat 文件

发布于 2025-01-06 15:08:42 字数 669 浏览 1 评论 0原文

我面临一个奇怪的问题,我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

春风十里 2025-01-13 15:08:42

你可以这样尝试。 ProcessStartInfo

psi3.RedirectStandardError= true;
psi3 .RedirectStandardOutput= true;
psi3.UseShellExecute= false;

you may try like this. ProcessStartInfo

psi3.RedirectStandardError= true;
psi3 .RedirectStandardOutput= true;
psi3.UseShellExecute= false;
执手闯天涯 2025-01-13 15:08:42

您可能需要额外的管理员权限才能执行批处理文件中的预期内容。如果是这样,请使用 Process.Start() 的替代版本:

Start(string fileName, string arguments, string userName, SecureString password, string domain);

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():

Start(string fileName, string arguments, string userName, SecureString password, string domain);
多孤肩上扛 2025-01-13 15:08:42

将批处理文件的第一行设置为:

Pushd %~dp1

这将设置批处理文件的工作目录。

马丁

Make the first line of the batch file this:

pushd %~dp1

This will set the batch files working directory.

martyn

笑饮青盏花 2025-01-13 15:08:42

这应该对您有用:

String myBatchFileName = "name_of_file.bat";
System.Diagnostics.Process.Start(myBatchFileName);
  • 请注意,变量 myBatchFileName 是 .bat 文件的实际名称......

This should work for you:

String myBatchFileName = "name_of_file.bat";
System.Diagnostics.Process.Start(myBatchFileName);
  • Note that variable myBatchFileName is the actual name of .bat file.....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文