[英]Console window still popping up even after ProcessWindowStyle.Hidden;
[英]Process with ProcessWindowStyle.Hidden still shows Press any key to exit?
我有這個:
var startInfo = new ProcessStartInfo
{
FileName = _pathToExe,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(_pathToExe),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
try
{
using (_proc = Process.Start(startInfo))
{
_proc.EnableRaisingEvents = true;
_proc.ErrorDataReceived += proc_DataReceived;
_proc.OutputDataReceived += proc_DataReceived;
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
var myStreamWriter = _proc.StandardInput;
var allArgs = "";
foreach (var arg in _args)
allArgs += arg + Environment.NewLine;
myStreamWriter.Write(allArgs);
_proc.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
我正在執行其他人編寫的 *.exe。 對於這個特定的 *.exe - 盡管您可以在上面清楚地看到我已經設置了 ProcessWindowStyle.Hidden,但我仍然看到一個黑色窗口出現,上面寫着“按任意鍵退出。”。 這個 *.exe - 如果我從命令行運行(而不是從我的 C# 代碼中調用它)會產生大量的控制台輸出文本。 當我運行我的 C# 代碼時,我沒有看到這個輸出文本,這是我想要的,也意味着重定向正在工作。
我檢查並完成了該過程 - 就好像命令窗口本身正在添加這個額外的(不需要的)步驟。
以前有沒有人遇到過這種情況,如果有,我該如何擺脫這種情況?
從文件:
要使用 System.Diagnostics.ProcessWindowStyle.Hidden,system.Diagnostics.ProcessStartInfo.UseShellExecute 屬性必須為 true。
您正在啟動的程序似乎在最后調用 system("PAUSE") 從而產生一個新進程,該進程打印“按任意鍵繼續...”消息並等待用戶輸入。 我無法重現你的確切情況,但你可以試試這個。
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
try
{
Process _proc;
using (_proc = Process.Start(startInfo))
{
_proc.EnableRaisingEvents = true;
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
var myStreamWriter = _proc.StandardInput;
myStreamWriter.WriteLine("D:\\your.exe"); //write your.exe to cmd and press enter :)
_proc.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.