【问题标题】:Start wpf application from console application从控制台应用程序启动 wpf 应用程序
【发布时间】:2019-07-23 09:21:43
【问题描述】:

我正在使用 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);

【问题讨论】:

标签: c#


【解决方案1】:

我认为这可能有效:

[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();
    }

【讨论】:

  • 我已经尝试过 setforegroundwindow ,它适用于任何 Windows 应用程序,但不适用于 wpf 应用程序。
  • 您是否尝试过将工作目录设置为 WPF 应用程序的路径,而不是启动应用程序的工作目录proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(programPath); where var programPath = @"C:\Users\user\Documents \程序目录\program.exe";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2016-03-07
  • 2017-05-21
相关资源
最近更新 更多