如何从 C# Windows 窗体应用程序执行 .bat 文件?

发布于 2024-07-11 23:13:49 字数 1650 浏览 12 评论 0原文

我需要做的是让 C# 2005 GUI 应用程序根据用户的请求调用 .bat 和几个 VBScript 文件。 这只是假期结束前的权宜之计,我可以用 C# 来编写它。 我可以毫无问题地执行 VBScript 文件,但无法执行 .bat 文件。 当我在 C# 应用程序中“单击”执行 .bat 文件时,DOS 窗口打开并关闭得非常快,并且测试 .bat 文件不执行 - “Windows 无法将 bat 识别为内部或外部命令”是错误返回到 DOS 框中。 如果我只是双击 .bat 文件或从命令提示符手动运行它,它就会执行。 我还需要 .bat 文件以静默方式执行,除非需要用户交互 - 该脚本将 11k+ 文件复制到联网计算机上的文件夹中,有时 Windows 会“忘记”目标是否是文件或目录,并要求用户告诉它它是什么(这是一个完全不同的问题,不在这里讨论......不用说我对此感到恼火)。

到目前为止,在我的 C# 源代码中,我有这样的内容:

Process scriptProc = new Process();


        if (File.Exists("c:\\scripts\\batchfile1.bat"))
        {

            scriptProc.StartInfo.FileName = @"cscript";
            scriptProc.StartInfo.Arguments = ("cmd.exe", "/C C:\\scripts\\batchfile1.bat"); // Wacky psuedo code //
            scriptProc.Start();
            scriptProc.WaitForExit(1500000);
            scriptProc.Close();

        }

        if (!File.Exists("c:\\scripts\\batchfile1.bat"))
        {
        }

我知道这段代码不起作用 - 但这本质上是我想要它做的事情。 我正在寻找类似的 .bat 文件。 我假设我必须告诉系统使用 cmd 来运行 .bat。 我不知道如何做到这一点。 我已经查看了这个用于 C# 2003 的 网站对我来说没有太大帮助,因为我对 C# 很陌生。

编辑:使用凯文的帖子我再次尝试。 与该帖子相同的解决方案脚本,但为我进行了修改,因为我不需要重定向:

 System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "C:\\scripts\\batchfile1.bat";
            proc.StartInfo.RedirectStandardError = false;
            proc.StartInfo.RedirectStandardOutput = false;
            proc.StartInfo.UseShellExecute = false;
            proc.Start();
            proc.WaitForExit();

What I need to do is have a C# 2005 GUI app call a .bat and several VBScript files at user's request. This is just a stop-gap solution until the end of the holidays and I can write it all in C#. I can get the VBScript files to execute with no problem but I am unable to execute the .bat file. When I "click" in the C# app to execute the .bat file a DOS window opens up and closes very fast and the test .bat file does not execute - "Windows does not recognize bat as an internal or external command" is the error returned in the DOS box. If I simply doubl-click the .bat file or manually run it from the command prompt it does execute. I also need the .bat file to execute silently unless a user interaction is required - this script copies 11k+ files to folders on a networked machine and occasionally Windows "forgets" if the destination is a file or directory and asks for the user to tell it what it is (this is a whole other issue not for discussion here...needless to say I am annoyed by it).

So far in my C# source I have this :

Process scriptProc = new Process();


        if (File.Exists("c:\\scripts\\batchfile1.bat"))
        {

            scriptProc.StartInfo.FileName = @"cscript";
            scriptProc.StartInfo.Arguments = ("cmd.exe", "/C C:\\scripts\\batchfile1.bat"); // Wacky psuedo code //
            scriptProc.Start();
            scriptProc.WaitForExit(1500000);
            scriptProc.Close();

        }

        if (!File.Exists("c:\\scripts\\batchfile1.bat"))
        {
        }

I am aware that this code does not work - but it is essentially what I want it to do. What I am looking at is something like this for .bat files. I assume I have to tell the system to use cmd to run the .bat. I am at a loss as to how to do this. I have checked out this site which is for C# 2003. Not much help for me as I am very green with C#.

EDIT: Using Kevin's post I attempted it again. Same solution script from that post but modified for me since I do not need to redirect:

 System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "C:\\scripts\\batchfile1.bat";
            proc.StartInfo.RedirectStandardError = false;
            proc.StartInfo.RedirectStandardOutput = false;
            proc.StartInfo.UseShellExecute = false;
            proc.Start();
            proc.WaitForExit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝咒 2024-07-18 23:13:49

这是您要查找的内容:

调用批处理文件后服务在 WaitForExit 处挂起

这是关于为什么服务无法执行文件的问题,但它显示了执行此操作所需的所有代码。

Here is what you are looking for:

Service hangs up at WaitForExit after calling batch file

It's about a question as to why a service can't execute a file, but it shows all the code necessary to do so.

寻找我们的幸福 2024-07-18 23:13:49

对于批处理文件询问用户目标是否是文件夹或文件的问题,如果您提前知道答案,可以这样做:

如果目标是文件:
回声 f | [批处理文件路径]

如果文件夹:
回声d | [批处理文件路径]

它本质上只是将“echo”之后的字母通过管道传输到批处理文件的输入。

For the problem you're having about the batch file asking the user if the destination is a folder or file, if you know the answer in advance, you can do as such:

If destination is a file:
echo f | [batch file path]

If folder:
echo d | [batch file path]

It will essentially just pipe the letter after "echo" to the input of the batch file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文