Question

In the application that I'm working on, everything works great. My question, is there a way to suppress the command windows when executing psexec? I want this to run silently. Below is the code that I'm using.. I've read many examples online but nothing seems to be working. Thoughts? Thanks.

            Process p = new Process();
            try
            {
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardInput = true;                                      
                p = Process.Start(psExec, psArguments);
                if (p != null)
                {
                    string output = p.StandardOutput.ReadToEnd();
                    string error = p.StandardError.ReadToEnd();
                    p.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (p != null)
                {
                    p.Dispose();
                }
            }
Was it helpful?

Solution

You're setting the StartInfo before actually assigning the p variable again, your code would have to look like this:

...
ProcessStartInfo startinfo = new ProcessStartInfo(psExec, psArguments);
startinfo.UseShellExecute = false;
startinfo.CreateNoWindow = true;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
startinfo.RedirectStandardInput = true;                                      
p = Process.Start(startinfo);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top