简体   繁体   中英

How to execute a bat file in ASP.NET C# and trace into it

I need to execute a bat file which calls a third party procedure, but firstable I tried to execute a simple bat which writes the output into a text file. The text file is not created and I am not able to trace throught it with breakpoints, can you help me?

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

This is the content for the bat file:

echo test > test.txt

Finally I managed the manner, I just needed to change the path:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM