C#:
private void go()
{
try
{
// Get the full file path
string strFilePath = @"C:\inetpub\wwwroot\ga\copyphone.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:\inetpub\wwwroot\ga\";
// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
// Open the batch file for reading
System.IO.StreamReader strm = new System.IO.StreamReader(strFilePath, System.Text.Encoding.GetEncoding(950));
//System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
//strm.CurrentEncoding.GetEncoder = System.Text.Encoding.GetEncoding(950);
//strm = System.IO.File.OpenText(strFilePath);
Response.Write(strm.CurrentEncoding.ToString());
// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;
// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;
// Write each line of 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();
string fmtStdOut = "{0}";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "
")));
}
catch (Exception ex)
{
throw ex;
}
}
沒有留言:
張貼留言