C# 如何隐藏另一个可执行文件的控制台窗口?
我们的C#应用程序将通过以下操作启动控制台应用程序可执行文件:C# 如何隐藏另一个可执行文件的控制台窗口?,c#,C#,我们的C#应用程序将通过以下操作启动控制台应用程序可执行文件:processcorrectionprocess=Process.Start(exePath,rawDataFileName) 客户希望隐藏对其应用程序无效的控制台窗口。我们可以这样做吗?您可以使用ProcessStartInfo创建流程,其中和属性分别设置为true和false ProcessStartInfo startInfo = new ProcessStartInfo(exePath); startInfo.CreateNo
processcorrectionprocess=Process.Start(exePath,rawDataFileName)代码>
客户希望隐藏对其应用程序无效的控制台窗口。我们可以这样做吗?您可以使用ProcessStartInfo
创建流程,其中和属性分别设置为true和false
ProcessStartInfo startInfo = new ProcessStartInfo(exePath);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.Arguments = rawDataFileName;
Process.Start(startInfo);
是的,这是可能的。您必须按照以下方式设置流程:
Process p = new Process();
p.StartInfo.FileName = exePath;
p.StartInfo.Arguments = rawDataFileName;
p.StartInfo.UseShellExecute = false;
p.CreateNoWindow = true;
// p.StartInfo.RedirectStandardOutput = true;
p.Start();
这就是我最近使用的代码:
ProcessStartInfo在系统中。诊断
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = "your_app.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "args"
//Launch the process.
process = Process.Start( startInfo );
您可以使用WinAPI隐藏任何控制台窗口,只要您知道它的名称。正在使用我的MSDN帖子
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
static void Main()
{
string caption = @"file:///";
caption += System.Reflection.Assembly.GetExecutingAssembly().Location;
caption = caption.Replace('\\','/');
// replace 'caption' with the exact caption of your console window
IntPtr hWnd = FindWindow(null, caption);
if (hWnd != IntPtr.Zero)
{
#region your code
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = " /c notepad.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.LoadUserProfile = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start();
#endregion
ShowWindow(hWnd, SW_HIDE); // Hide console window
myProcess.WaitForExit();//OPTIONAL
//ShowWindow(hWnd, SW_SHOW); // Reshow console window (OPTIONAL)
}
}
}
这个怎么样:
private void StartHiddenConsoleApp( string exePath, string args )
{
var psi = new ProcessStartInfo();
psi.Arguments = args;
psi.CreateNoWindow = false;
psi.ErrorDialog = false;
psi.FileName = exePath;
psi.WindowStyle = ProcessWindowStyle.Hidden;
var p = Process.Start( psi );
}
你有你要启动的EXE的源代码吗?我相信OP的意思是另一个进程。在这种情况下,工艺参数将不起作用。标题alsp指定“来自另一个exe”。OP要求隐藏由另一个应用程序创建的另一个exe的控制台窗口。只有当他能够用这个调用者包装应用程序时,这才有效。此外,应用程序并不总是响应CreateNoWindow参数。@P.Brian.Mackey-OP发布了使用Process.Start
的清晰示例。我只能假设这是一个使用场景。很好。我假设Process.Start用于他的应用程序exe,他想隐藏另一个应用程序exe。这个问题还不清楚,但很明显,当OP选择您的答案时,您是对的。您实际上必须将psi.CreateNoWindow设置为true,而不是false。