从控制台应用程序启动WPF应用程序

时间:2019-07-23 09:21:43

标签: c#

我使用process.start启动wpf应用程序,我的wpf应用程序在任务管理器中可见,但在前端不可见,它作为后台进程运行,可以帮助使wpf应用程序可见

ProcessStartInfo startInfo = new ProcessStartInfo(programFilesPath);
//ProcessStartInfo startInfo = new 
ProcessStartInfo("notepad.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
Process p = Process.Start(startInfo);

1 个答案:

答案 0 :(得分:0)

我认为这可能有效:

[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
public static void Processing(string WorkingDirectory, string FileName, string Arguments, bool StandardOutput, string OutputFileName)
    {
        Process proc = new Process();
        proc.EnableRaisingEvents = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = StandardOutput;
        proc.StartInfo.FileName = FileName;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.WorkingDirectory = WorkingDirectory;
        proc.StartInfo.Arguments = Arguments;
        proc.Start();
        //Added code
        System.Threading.Thread.Sleep(500);
        SetForegroundWindow(proc.MainWindowHandle);
        //........................................
        if (StandardOutput == true)
        {
            string output = proc.StandardOutput.ReadToEnd();
            DumpOutput(WorkingDirectory + "\\" + OutputFileName, output);
        }
        proc.WaitForExit();
        proc.Close();
    }