. NET Execute cmd command

Source: Internet
Author: User

Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Diagnostics;

Namespace WebForm
{
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Response.Write (Execommand ("ping www.126.com"));
}

public string Execommand (string commandtext)
{
Process P = new process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false;
P.startinfo.redirectstandardinput = true;
P.startinfo.redirectstandardoutput = true;
P.startinfo.redirectstandarderror = true;
P.startinfo.createnowindow = true;
string stroutput = null;
Try
{
P.start ();
P.standardinput.writeline (CommandText);
P.standardinput.writeline ("Exit");
Stroutput = P.standardoutput.readtoend ();
p.WaitForExit ();
P.close ();
}
catch (Exception e)
{
Stroutput = E.message;
}
return stroutput;
}
}
}

WaitForExit

In the program I use the Process class to start the command line, execute the batch file ' Create.cmd ' (when I manually drag this file into the command line execution, everything is OK). C # program code is similar to the following, where the Batchfilepath variable is the full path of the batch file:

Code

M_basicdataproc = new Process ();

M_BasicDataProc.StartInfo.FileName = "cmd.exe";

M_BasicDataProc.StartInfo.CreateNoWindow = false;

M_BasicDataProc.StartInfo.UseShellExecute = false;

M_BasicDataProc.StartInfo.RedirectStandardOutput = true;

M_BasicDataProc.StartInfo.RedirectStandardInput = true;

M_BasicDataProc.StartInfo.WorkingDirectory = Path.getdirectoryname (Batchfilepath);

M_basicdataproc.start ();

String batchfilename = Path.getfilename (Batchfilepath);

StreamWriter InputStream = m_basicdataproc.standardinput;

Inputstream.writeline (Batchfilename);

Inputstream.close ();

M_basicdataproc.waitforexit ();

M_basicdataproc.enableraisingevents = true;

The batch file ' Create.cmd ' calls ' Sqlplus ' to execute several SQL files:

//===================================================

echo Tables on Level 0:

If exist Installscripts\create01.sql (

echo Bas

Sqlplus%1/%[email protected]%3 @InstallScripts \create01.sql | %hidesqlplusrows%

REM > Logs\create_%1.txt

)

If exist Installscripts\create02.sql (

......

//===================================================

The problem that arises is that the program runs to ' M_basicdataproc.waitforexit (); ' In this line, the clitoris will not move.

After two days, the last discovery was that there was a deadlock. Because the standard output stream is redirected, and the buffer size of the process.standardoutput is limited (said to be 4k), the child process (Cmd.exe) waits for the main process when the buffer is full (there is a lot of output to execute the batch file above) (C # APP) reads and releases this buffer, and the main process, because the WaitForExit () method is called, waits for the child process to exit and finally forms a deadlock.

After understanding the cause, there are 3 ways to solve the problem:

1) Modify the batch file, when calling Sqlplus, the output is assigned to a log file, so that the content being directed to the standardoutput is relatively small, it is not easy to cause problems:

//===================================================

echo Tables on Level 0:

If exist Installscripts\create01.sql (

echo Bas

Sqlplus%1/%[email protected]%3 @InstallScripts \create01.sql | %hidesqlplusrows% > Logs\create_%1.txt

)

......

//===================================================

2) Modify the C # code to ' m_BasicDataProc.StartInfo.RedirectStandardOutput = false; ', so that all output will be output directly on the command line screen and will not be redirected to the standard output stream.

3) Modify C # code, in ' M_basicdataproc.waitforexit (); ' Before adding ' m_basicdataproc.beginoutputreadline (); ' or ' m_BasicDataProc.StandardOutput.ReadToEnd (); ', by reading the output stream in order to release the appropriate buffer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.