我正在嘗試在我的 .aspx 網頁上執行 .bat 檔案我嘗試的是這個
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Application["Loaded"] == null)
{
Application["Loaded"] = 1;
}
else
{
int visits = (int)Application["Loaded"];
visits ;
Application["Loaded"] = visits;
if (visits > 50)
{
ProcessStartInfo ps = new ProcessStartInfo();
//ps.RedirectStandardOutput = true;
ps.UseShellExecute = false;
ps.WindowStyle = ProcessWindowStyle.Hidden;
Process P = new Process();
P.StartInfo.CreateNoWindow = true;
Process.Start(@"C:\Users\percoid it\Documents\Tempremover.bat");
Application["Loaded"] = 1;
}
}
}
}
我確實嘗試了所有這些方法和程序,但仍然彈出控制臺視窗。
我正在嘗試洗掉存盤在我的 PC 中的所有臨時檔案,并且.bat在加載我的 Crystal Report 超過 50 次后檔案確實洗掉了。
問題是,如果我加載報告的次數超過 50 次,我的報告確實崩潰了,并且在添加.bat檔案后它洗掉了 Crystal Report 存盤的臨時檔案。
但這里的問題只是在執行時彈出那個批處理檔案
uj5u.com熱心網友回復:
您在System.Diagnostics.Process 中面臨的問題是您已經創建了ProcessStartInfo的實體,但尚未使用它。
請嘗試以下操作:
string exePath = @"C:\Users\percoid it\Documents\Tempremover.bat";
string arguments = null;
//create new instance
ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
startInfo.Arguments = arguments; //arguments
startInfo.CreateNoWindow = true; //don't create a window
startInfo.RedirectStandardError = true; //redirect standard error
startInfo.RedirectStandardOutput = true; //redirect standard output
startInfo.RedirectStandardInput = false;
startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.ErrorDialog = false;
//create new instance
using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
{
//subscribe to event and add event handler code
p.ErrorDataReceived = (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Debug.WriteLine("Error: " e.Data);
}
};
//subscribe to event and add event handler code
p.OutputDataReceived = (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Debug.WriteLine("Output: " e.Data);
}
};
p.Start(); //start
p.BeginErrorReadLine(); //begin async reading for standard error
p.BeginOutputReadLine(); //begin async reading for standard output
//waits until the process is finished before continuing
p.WaitForExit();
}
資源:
- ProcessStartInfo 類
- Process.Start方法
- 行程類
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/392212.html
上一篇:特殊字符后批量替換字串
