asp.net call cmd.exe

Source: Internet
Author: User

See the code on the Internet, slightly done under the modified, the folder will be operating permissions set to everybody-Full Control
private void Runcmd ()
{
StreamReader sOut = null;
StreamWriter sIn = null;
Try
{
ProcessStartInfo psi = new ProcessStartInfo ("cmd.exe");
Psi. UseShellExecute = false;
Psi. Redirectstandardoutput = true;
Psi. Redirectstandardinput = true;
Psi. Redirectstandarderror = true;
Psi. WorkingDirectory = Mobilepath;
Process proc = Process.Start (PSI);
SOut = Proc. StandardOutput;
SIn = Proc. Standardinput;
String builderxml = String. Format (@ "mkdir c:/test22");
Sin.writeline (Builderxml);
Sin.writeline ("EXIT");
Proc. Close ();
}
catch (Exception ex)
{
Return
}
Finally
{
Sin.close ();
Sout.close ();
}
}


Relevant information on the Internet
Http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx

Run A. BAT file from asp.net
Okay, running.  BAT files from asp.net using the System.Diagnostics.Process object and static methods this should is easy, right? So, this is might work for you, but it certainly won ' t work in my machines. And after doing lots of Reasearh on the issue, it seems that other people are have also with this problems.

I wrestled with permissions and all sorts of the other stuff, trying to get a simple batch file to run, with no luck. I tried lauching The bat file directly, launching Cmd.exe and calling the bat file using Stin.  No Dice.  It seems that something in my machine is keeping a unattended process from running bat files. This makes sense, but I is never able to pinpoint what is preventing this, so I came up with a workaround.

I realized that since I could sucessfully run Cmd.exe, and send commands to it via stin, I could just open the batch file,  and send to Cmd.exe, which is essentially the same as running a batch file itself. This technique works great, and I thought I ' d pass along the code here.

Get the full file path
String strFilePath = "C://temp//test.bat";

Create the ProcessInfo Object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo ("cmd.exe");
Psi. UseShellExecute = false;
Psi. Redirectstandardoutput = true;
Psi. Redirectstandardinput = true;
Psi. Redirectstandarderror = true;
Psi. WorkingDirectory = "c://temp//";

Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start (PSI);


Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText (strFilePath);

Attach the output for reading
System.IO.StreamReader sOut = Proc. StandardOutput;

Attach the in for writing
System.IO.StreamWriter sIn = Proc. Standardinput;


Write the batch file to standard input
while (STRM. Peek ()!=-1)
{
Sin.writeline (STRM. ReadLine ());
}

Strm. Close ();

Exit CMD. Exe
String stechofmt = "# {0} run successfully. Exiting ";

Sin.writeline (String.Format (STECHOFMT, strFilePath));
Sin.writeline ("EXIT");

Close the process
Proc. Close ();

Read the SOut to a string.
String results = Sout.readtoend (). Trim ();


Close the IO Streams;
Sin.close ();
Sout.close ();


Write out the results.
String fmtstdout = "<font face=courier size=0>{0}</font>";
This. Response.Write (String.Format) (fmtstdout,results. Replace (System.Environment.NewLine, "<br>"));

After testing, the above read bat in Windows 2003 is normal, but put in 2000, always can not execute this bat, and finally have to directly enter the code in the command, which reason has not been found, the problem is estimated to be permissions, hehe.

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.