question

AnkiItwizard5-8193 avatar image
0 Votes"
AnkiItwizard5-8193 asked ·

procStartInfo.RedirectStandardOutput not working properly?

procStartInfo.RedirectStandardOutput not working properly i don't know why?
my code:
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/q /c " + batchFile);
procStartInfo.RedirectStandardOutput = true;

                 procStartInfo.RedirectStandardError = true;
                 procStartInfo.UseShellExecute = false;
                 procStartInfo.CreateNoWindow = true;

                 Process proc = new Process();
                 proc.StartInfo = procStartInfo;
                 proc.StartInfo.CreateNoWindow = true;
                 proc.Start();
                 string result_prog = proc.StandardOutput.ReadToEnd(); // get result null
dotnet-csharpdotnet-runtimedotnet-runtime-core
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Do you see the expected output in this test: Process.Start( "cmd", "/q /k " + batchFile)?

0 Votes 0 ·

No. I did't get desired output.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

Here is an example that may help with whatever you are attempting. The code assumes PowerShell is in the path, gets the IP address without showing a window to a text file and reads back the results once the PowerShell code finishes executing using a language extension.

This method will keep a windows form responsive and if this is not needed process.WaitForExit can be used.

 public static class Extensions
 {
     public static Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
     {
         if (process.HasExited)
         {
             return Task.CompletedTask;
         }
    
         var tcs = new TaskCompletionSource<object>();
            
         process.EnableRaisingEvents = true;
         process.Exited += (sender, args) => tcs.TrySetResult(null);
    
         if (cancellationToken != default)
         {
             cancellationToken.Register(() => tcs.SetCanceled());
         }
    
         return process.HasExited ? Task.CompletedTask : tcs.Task;
     }
 }


Form code

 private async void button1_Click(object sender, EventArgs e)
 {
     PowerShellScriptExecute();
 }
 private string _fileName = "ipPower.txt";
 public async void PowerShellScriptExecute()
 {
     var start = new ProcessStartInfo
     {
         FileName = "powershell.exe",
         UseShellExecute = false,
         RedirectStandardOutput = true,
         Arguments = "Invoke-RestMethod ipinfo.io/ip", 
         CreateNoWindow = true
     };
    
     using var process = Process.Start(start);
     using var reader = process.StandardOutput;
        
     process.EnableRaisingEvents = true;
        
     var ipAddressResult = reader.ReadToEnd();
        
     await File.WriteAllTextAsync(_fileName, ipAddressResult);
     await process.WaitForExitAsync();
    
     Debug.WriteLine(await File.ReadAllTextAsync(_fileName));
 }


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

Please try if this code works for you:

             string file = @"D:\test\txt\test.bat";
             var process = new Process();
             var startinfo = new ProcessStartInfo(file);
             startinfo.RedirectStandardOutput = true;
             startinfo.UseShellExecute = false;
             process.StartInfo = startinfo;
    
             process.OutputDataReceived += (sender, argsx) => Console.WriteLine(argsx.Data); 
             process.Start();
             process.BeginOutputReadLine();
             process.WaitForExit();

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.