4

Gurus,

I am struggling with a basic function in C#. I am not a develop expert, but I want to develop a PoC for a given customer requirement. Your guidance is welcome.

I am using the following peace of code, however, it is not working as expected.

I want to open psr.exe minimized, but looks like the windows style has been ignored.

ProcessStartInfo proc = new ProcessStartInfo("psr.exe");
proc.Arguments = "/start /output ....;
proc.WindowStyle = ProcessWindowStyle.Minimized;
proc.RedirectStandardOutput = true;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process.Start(proc);

Thanks in advance.

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • 1
    try changing it to `ProcessWindowStyle.Hidden` – MethodMan May 31 '17 at 18:01
  • @MethodMan thanks for your answer. I've tried, but I got the same result. The psr window is still showing up. – Edson Ferreira May 31 '17 at 18:04
  • then do a google search on the following `C# stackoverflow RedirectStandardOutput` plenty of good working examples. – MethodMan May 31 '17 at 18:11
  • `UseShellExecute = false` probably is the problem. Setting it to true is to use the OS shell to start the application – FortyTwo May 31 '17 at 18:12
  • My previous comment was incorrect, I meant to say "it works if `UseShellExecute=true`, but you can't redirect output with that setting". If you don't need the output then just do that. Another option is to use the dllImport [here](https://social.msdn.microsoft.com/Forums/vstudio/en-US/779ccb93-c9cd-49a5-881b-9218cbdf3aa5/minimize-a-systemdiagnosticsprocess?forum=csharpgeneral), where you need `var theProcess = Process.Start()` then `ShowWindow(theProcess.MainWindowHandle, 2)`; you have to `Thread.Sleep(some amount of time)` to wait for it the main window to be available, though. – Quantic May 31 '17 at 18:23
  • Thank you all for the guidance. Actually, I've tried all recommendations and it still not working in C#. Also, I tried in another language such as js.net and it works. Perhaps, it is something in my workstation or something like that. Here is the solution I founded. I've added an additional argument "/gui 0" and psr has opened with no windows. Again thanks. – Edson Ferreira May 31 '17 at 18:35

1 Answers1

1

Use shellExecute true and run the application with admin privilges,

string fileName = @"C:\Program Files (x86)\RequiredApp.exe";
Process process = new Process();
process.StartInfo.Arguments = null;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo.FileName = fileName;
process.StartInfo.UseShellExecute = true;
process.Start();
Mahesh
  • 823
  • 1
  • 11
  • 29