Jump to content

Using multiple commands through CMD using C#?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
bloodfire

bloodfire

    Newbie

  • Members
  • Pip
  • 4 posts
Hi! Currently, I have this code of trying to use CMD through C#:


Process p = new Process();

string vamppath = "set VAMP_PATH=D:";

string args = "echo %VAMP_PATH%";

p.StartInfo = new ProcessStartInfo("cmd", vamppath)

{

   RedirectStandardOutput = true,

   RedirectStandardInput = true,

   UseShellExecute = false,

   CreateNoWindow = true

};


//my alternate initialization code, but also does not work

//p.StartInfo.FileName = "cmd";

//p.StartInfo.Arguments = args;

//p.StartInfo.RedirectStandardOutput = true;

//p.StartInfo.RedirectStandardInput = true;

//p.StartInfo.UseShellExecute = false;

//p.StartInfo.CreateNoWindow = true;


p.Start();

p.StandardInput.WriteLine(vamppath);

string output = p.StandardOutput.ReadToEnd();

Console.WriteLine("Setting VAMP_PATH: " + output);

p.WaitForExit();

p.StandardInput.WriteLine(args);

output = p.StandardOutput.ReadToEnd();

p.WaitForExit();

Console.WriteLine("Get VAMP_PATH: " + output);


I just do a simple set and get of a particular environment variable. When I do it manually in CMD it works properly. But when I execute the following commands using C# it does not.

What could be the problem? Thanks!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
From what I can gather, you are using writeline, and thus breaking the D: path with a new line.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
bloodfire

bloodfire

    Newbie

  • Members
  • Pip
  • 4 posts
Hi!

Sorry, but I tried using just .Write and still it does not work properly.

To try and debug, I used breakpoint at the first p.Start(); then use Step-Into to see which part it seems to error in.

I discovered that when I reach the 'string output = p.StandardOutput.ReadToEnd();' line, it goes back to my program and just seems to hang (like an infinite loop) and does not continue on to the other lines.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users