2017-10-12 24 views
0

在執行「proc.StandardOutput.ReadToEnd()」後,在Web應用程序中執行了控制檯應用命令「sw-precache」並輸出了預期的結果。 WebApplication和ConsoleApplication有什麼區別?c#執行WebApplication中的Process和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); 
+2

第一個區別可能是用戶執行該命令。第二件事是執行實際的命令,而不是執行'cmd'並傳遞可執行文件。 – Filburt

+1

[從.NET應用程序捕獲控制檯輸出(C#)]的可能重複(https://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) – DiskJunky

回答

0

您的IIS Worker將執行代碼將不會默認擁有這樣的權限,由於安全考慮,授予這些權限並不是一個好習慣。

您可以藉助模擬來查看執行進程,並且最好只允許執行該特定進程等,而不是具有系統範圍管理權限的帳戶。