Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何启动流程并将其隐藏';窗户在哪里?_C#_.net_Winforms - Fatal编程技术网

C# 如何启动流程并将其隐藏';窗户在哪里?

C# 如何启动流程并将其隐藏';窗户在哪里?,c#,.net,winforms,C#,.net,Winforms,我试过这个: ProcessStartInfo psi = new ProcessStartInfo("https://stackoverflow.com/"); psi.RedirectStandardOutput = false; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = false; Process.S

我试过这个:

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

            Process.Start(psi);
但我在生产线上遇到了异常。开始(psi)

Win32Exception系统找不到指定的文件

如果我更改行psi.UseShellExecute=true; 然后它工作了,但它没有隐藏窗口

我希望当它打开broswer时,例如,用户在任何时候都不会看到窗口,但窗口仍然会打开。不是关闭而是隐藏它

试图用谷歌搜索,但没有找到有效的解决方案

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);

您可以从
ApplicationOpenForms
中找到所有打开的表单,然后可以隐藏所有表单,但存在一个问题,该数组是刚打开表单的列表,当其中一个表单关闭或隐藏时,该表单将从列表中删除,并且For或Foreach循环将异常

测试此代码以在打开URL后隐藏所有打开的表单:

    Process.Start("http://stackoverflow.com/");

    List<Form> oForms = new List<Form>();
    foreach (Form form in Application.OpenForms)
    {
        oForms.Add(form);
    }

    foreach (var form in oForms)
    {
        form.Hide();
    }
Process.Start(“http://stackoverflow.com/");
表单列表=新列表();
foreach(Application.OpenForms中的表单)
{
添加(表格);
}
foreach(oForms中的var形式)
{
form.Hide();
}

”http://stackoverflow.com/“
不是可执行文件的有效路径。如果要打开URL,必须在其中指定浏览器可执行文件。但是隐藏浏览器并没有多大意义…请参阅。与OP的原始代码不同,这不会使用默认的web浏览器打开URL。如果不想硬编码特定web浏览器的可执行文件名,请参阅以确定默认web浏览器的文件路径。