gpt4 book ai didi

c# - 我如何启动进程并隐藏它的窗口?

转载 作者:行者123 更新时间:2023-11-30 12:25:52 25 4
gpt4 key购买 nike

我试过这个:

ProcessStartInfo psi = new ProcessStartInfo("https://stackoverflow.com/");
psi.RedirectStandardOutput = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;

Process.Start(psi);

但我在 Process.Start(psi); 线上遇到异常

Win32Exception 系统找不到指定的文件

如果我更改行 psi.UseShellExecute = true;然后它可以工作,但它不会隐藏窗口。

我希望当它打开浏览器时,例如 https://stackoverflow.com/用户在任何时候都不会看到该窗口,但该窗口仍将打开。不是关闭而是隐藏。

尝试谷歌但没有找到可行的解决方案。

Win32Exception 消息:

System.ComponentModel.Win32Exception was unhandled
HResult=-2147467259
Message=The system cannot find the file specified
Source=System
ErrorCode=-2147467259
NativeErrorCode=2
StackTrace:
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at CuteDadyImages.Form1.OpenBroswerTab() in d:\C-Sharp\test\Form1.cs:line 155
at CuteDadyImages.Form1..ctor() in d:\C-Sharp\test\Form1.cs:line 55
at CuteDadyImages.Program.Main() in d:\C-Sharp\test\Program.cs:line 35
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

最佳答案

在代码的某处添加以下内容

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

然后尝试使用以下命令启动浏览器:

        var process = new Process
{
StartInfo =
{
FileName = "firefox.exe",
Arguments = "http://stackoverflow.com/",
CreateNoWindow = true,
ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden
}
};
process.Start();
Thread.Sleep(1000);
ShowWindow(process.MainWindowHandle, 0);

关于c# - 我如何启动进程并隐藏它的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30414141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com