0

I have a list of a thousand items, Each of these items must be checked by the CMD.exe, With the help of the following code, I can check an item by CMD

  var p = new Process
 {
   StartInfo =
   {
     UseShellExecute = false,
     RedirectStandardOutput = true,
     CreateNoWindow = true,
     FileName = "cmd",
     Arguments = $"list {Id}"
   }};
   p.Start();
   var _Data = await p.StandardOutput.ReadToEndAsync();

But the question is, I want all of these items to be checked quickly by CMD, I'm currently doing this as follows

foreeach(var item in list)
{
     var p = new Process
     {
       StartInfo =
       {
         UseShellExecute = false,
         RedirectStandardOutput = true,
         CreateNoWindow = true,
         FileName = "cmd",
         Arguments = $"list {item}"
       }};
       p.Start();
       var _Data = await p.StandardOutput.ReadToEndAsync();
}

But it takes a long time to do this

1
  • I beg to suggest you are probably going the wrong way about this altogether. This seems to be a classic XY Problem. What are you actually trying to achieve? Commented Jun 13, 2021 at 11:50

1 Answer 1

2

You can redirect standard input and use a StreamWriter to write to it:

Process p = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.RedirectStandardInput = true;
    info.UseShellExecute = false;

    p.StartInfo = info;
    p.Start();

    using (StreamWriter sw = p.StandardInput)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("mysql -u root -p");
            sw.WriteLine("mypassword");
            sw.WriteLine("use mydb;");
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.