0

I have a web app c # hosted on IIS on a computer with windows server 2008, I ran a command on a windows server cmd through C#, but it doesn't work, I tried it locally on my computer and the command works, I don't know why it doesn't work on the computer with windows server, I use this source code,I put a log but doesn't throw any error.

        protected void btnReboot_Click(object sender, EventArgs e)
        {
            try
            {

                //StartShutDown("-l");
                StartShutDown("-f -r -t 5");

                Log2("MNS OK");
            }
            catch (Exception ex)
            {
                Log2("MNS ERROR  " + ex.ToString());
            }

        }
        private static void StartShutDown(string param)
        {
            ProcessStartInfo proc = new ProcessStartInfo();
            proc.FileName = "cmd";
            proc.WindowStyle = ProcessWindowStyle.Hidden;
            proc.Arguments = "/C shutdown " + param;
            Process.Start(proc);
        }

7
  • Change /C in /K and comment out the line proc.WindowStyle Now you should be able to see the console windows opened by the command and you can look if there is an error message Commented Jan 13, 2020 at 22:57
  • @Steve It doesn't open any command window Commented Jan 13, 2020 at 23:20
  • Did you comment out the line the hides the command window? Commented Jan 13, 2020 at 23:24
  • And are you able to log on your server and try to open a command window using same account used to launch the Process? Commented Jan 13, 2020 at 23:27
  • @Steve on the server if it is possible to open the command window manually and I comment out the line proc.WindowStyle Commented Jan 13, 2020 at 23:41

1 Answer 1

1

You can actually capture the error output from the process that was launched by redirecting the standard error. An example would be like this:

private static void StartShutDown(string param)
{
    Process p = new Process();
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true; // You need to set this
    p.StartInfo.UseShellExecute = false; 

    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = "/C shutdown " + param;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.Start();

    string stdoutx = p.StandardOutput.ReadToEnd();         
    string stderrx = p.StandardError.ReadToEnd(); // here is where you get the error output string        
    p.WaitForExit();

    Console.WriteLine("Exit code : {0}", p.ExitCode);
    Console.WriteLine("Stdout : {0}", stdoutx);
    Console.WriteLine("Stderr : {0}", stderrx);
}

Once you have the Stderr you can check its contents and, if it's not empty then you know an error occurred.

Sign up to request clarification or add additional context in comments.

1 Comment

On the Stderr line it shows me access denied

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.