0

In Console Application command "sw-precache" is executed and output contains the expected result, in web application after execute "proc.StandardOutput.ReadToEnd()" nothing happening! What can be the difference between WebApplication and ConsoleApplication?

Process proc = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = @"cmd.exe",
                        Arguments = @"/c some-command",
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        CreateNoWindow = true,
                        WorkingDirectory = Path.GetFullPath(appPathDir)
                    }
                };


                proc.Start();

    // To avoid deadlocks, always read the output stream first and then wait.
                string output = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
                Console.WriteLine("success");
                Console.WriteLine(output);
Alex
  • 8,908
  • 28
  • 103
  • 157
  • 2
    The first difference is likely the user executing the command. The second thing would be to execute the actual command instead of executing a `cmd` and passing the executable. – Filburt Oct 12 '17 at 15:20
  • 1
    Possible duplicate of [Capturing console output from a .NET application (C#)](https://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) – DiskJunky Oct 12 '17 at 15:34

1 Answers1

0

Your IIS Worker who will be executing the code will not by default have the permissions to do so and it's not a good practice to give these rights due to security concerns.

You could look at executing the process with help of impersonation, and preferable only allowed to execute that specific process etc and not an account with system wide admin rights.

StefanE
  • 7,578
  • 10
  • 48
  • 75