|
-
May 19th, 2011, 10:57 PM
#1
How to use multiple CMD commands through C#?
Hi! Currently, I have this code of trying to use CMD through C#:
Code:
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!
-
May 19th, 2011, 11:47 PM
#2
Re: How to use multiple CMD commands through C#?
I think you are looking for something more along the lines of
Code:
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "set";
proc.Arguments = "VAMP_PATH=D:";
This will issue 'set VAMP_PATH=D:' to the command line you do not need to call cmd.
Also since you were trying to call cmd and using WaitForExit you will be stuck in a loop until you actual exit the command window.
If your app is sending multiple items to command you can just create 1 usable method to use.
Code:
private void SendToCMD(string cmd, string args)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = cmd;
proc.Arguments = args
...etc
...etc
}
Last edited by Omegadarkest; May 20th, 2011 at 12:03 AM.
-
May 20th, 2011, 02:08 AM
#3
Re: How to use multiple CMD commands through C#?
Hi!
I get an error when I remove the 'cmd.exe' regarding the filename. Something about there is no filename specified when I try to start the process.
-
May 20th, 2011, 08:19 AM
#4
Re: How to use multiple CMD commands through C#?
Sorry it was like 1am since you are using c# why don't you just use.
Code:
System.Environment.SetEnvironmentVariable
System.Environment.GetEnvironmentVariable
as the other method you can use
Code:
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "set VAMP_PATH=D:";
If you want a permanent variable use setx
Last edited by Omegadarkest; May 20th, 2011 at 08:26 AM.
-
May 20th, 2011, 09:05 AM
#5
Re: How to use multiple CMD commands through C#?
Hi!
Hey great! That works! I didnt know about the EnviromentVariable since Im pretty new.
Anyway, thank you very much again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|