0

This is my code and I'm using it in a while loop. The cmd command changes every time. It means if I write "cd .." and in the next round of while loop if I write "dir" that's not gonna give me the previous folder items or in simpler language previous cmd closed and another one opens.

while (i < 99) {
    System.Diagnostics.Process process = new 
    System.Diagnostics.Process();
    process.StartInfo.WindowStyle = 
    System.Diagnostics.ProcessWindowStyle.Hidden;
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.Arguments = @"/C " + lastMsg; //this line is a cmd command
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardInput = true;
    process.Start();

    string q = "";
    while (!process.HasExited)
    {
        q += process.StandardOutput.ReadToEnd();
    }
}
  • Why do you use the C# [Process Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process) being a C# wrapper class for the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) used with [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure to run `cmd.exe` which uses also `CreateProcess` and `STARTUPINFO` to run executables? – Mofi Feb 19 '22 at 11:44
  • That makes absolutely no sense. There can be used the C# `Process` class itself to run all executables which should be run either one after the other or all parallel (not good on 99 programs). The current directory for each process can be defined with the property `WorkingDirectory` of the [ProcessStartInfo Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo). There is absolutely no need to run `cmd.exe` ever from within a C# coded application as all Windows library functions used by `cmd.exe` can be used also by the C# coded application. – Mofi Feb 19 '22 at 11:58
  • After having read all the Microsoft documentation pages referenced in my first comment you should also know the difference between __current directory__ and __program directory__. The process starting a program defines the current directory. It can be the program directory, but it can be also any other directory. So a C# coded program should never expect that the directory containing the program is the current directory on execution. There should be used C# code to get the directory path of the the C# coded program and use it on referencing files/folders in the program directory. – Mofi Feb 19 '22 at 11:59
  • See: [Which is better for getting assembly location: GetAssembly().Location or GetExecutingAssembly().Location?](https://stackoverflow.com/a/27060089/3074564) and [How can I get the application's path in a .NET console application?](https://stackoverflow.com/questions/837488/) and [Best way to get application folder path](https://stackoverflow.com/questions/6041332) and please ignore all answers which get the current directory path and think this path is always equal the program path which is definitely a completely wrong expectation. – Mofi Feb 19 '22 at 11:59
  • doing a cd .. and then dir, such as to traverse some path, there are already C# functions that handle that. Any better context of what you are trying to do? You are not capturing the redirection (unless elsewhere). Is your "command" something other than directory traversing? – DRapp Feb 19 '22 at 12:50

1 Answers1

0

If you want to send all commands to the same instance of cmd, you must create this instance outside your loop, redirect the standard input, and send the commands through the standard input:

var cmdStartInfo = new ProcessStartInfo
{
    FileName = "cmd",
    RedirectStandardInput = true,
    WindowStyle = ProcessWindowStyle.Hidden,
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardOutput = true
};

var cmdProcess = Process.Start(cmdStartInfo);

while (i < 99)
{
    cmdProcess.StandardInput.WriteLine(lastMsg);
}
Jann Westermann
  • 291
  • 1
  • 2