1

At the moment I'm developing a small wrapper around CMD using ProcessStartInfo, trying to emulate the command window and adding some extra functionality that I desperately need.

This WINFORM application is a multitab app so that you can start multiple 'sessions'. Furthermore, since the content is stored in a richtextbox, I can easily copy it and more importantly search the console log.

Up till now everything seems to work fine, but when I ask for the current directory, I see the location of this app, which is fine. But when I go one directory up, it does not seem to work.

I'm pretty sure I'm doing something wrong. Could somepoint point me out what it is, what I'm doing wrong? Here is the code excerpt that deals with the execution of the code.

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
// Do not create window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(onOutputDataReceived);
proc.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(onErrorDataReceived);
proc.Start();
proc.StandardInput.WriteLine("/c " + cmd);
proc.StandardInput.Close();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
Otiel
  • 18,404
  • 16
  • 78
  • 126
Ruud
  • 11
  • 1
  • 1
    You need to be more specific than "does not work". – nvoigt May 26 '13 at 09:48
  • You are right. What I notice is that the directory remains the same. If I type cd .. then the current directory does not go one up. Also when I explicitly use an absolute path, like cd c:\temp, it also does not give me the expected result: the current directory remains the same. – Ruud May 26 '13 at 09:53

1 Answers1

0

You seem to be starting a new process each time you want to execute a command. If you want the environment (like current path) to stay the same between commands, you will need to either save the environment and load it into your new process, or keep a single process open.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Aha, thank you. I was not aware of this. That explains that weird behaviour (which is not that weird after all ;-). I will make the necessary modifications. – Ruud May 26 '13 at 10:32