【问题标题】:How to execute a bat file in ASP.NET C# and trace into it如何在 ASP.NET C# 中执行一个 bat 文件并跟踪它
【发布时间】:2018-04-16 16:07:57
【问题描述】:

我需要执行一个调用第三方程序的 bat 文件,但首先我尝试执行一个简单的 bat,它将输出写入文本文件。 文本文件未创建,我无法通过断点跟踪它,您能帮帮我吗?

System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = @"c:\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = Server.MapPath("Temp/test.bat");
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
//string output = si.StandardOutput.ReadToEnd();
si.Close();

这是bat文件的内容:

echo test > test.txt

【问题讨论】:

  • 确保您使用的是 IIS 应用程序池用户拥有写入权限的路径。

标签: c# asp.net shell process system.diagnostics


【解决方案1】:

最后我管理的方式,我只需要改变路径:

                    string path = Server.MapPath("Temp");
                    System.Diagnostics.Process proc = new System.Diagnostics.Process();
                    proc.StartInfo.WorkingDirectory = path;
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.FileName = PCombine(path, "test.bat");
                    proc.StartInfo.Arguments = String.Format("{0} {1}", PCombine(path, filename), PCombine(path, "new_" + filename));
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.Start();
                    proc.WaitForExit();
                    proc.Close();
                    proc.Dispose();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2013-08-01
    • 1970-01-01
    • 2013-04-25
    • 2015-04-03
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多