CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    6

    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!

  2. #2
    Join Date
    Dec 2009
    Posts
    18

    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.

  3. #3
    Join Date
    Nov 2009
    Posts
    6

    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.

  4. #4
    Join Date
    Dec 2009
    Posts
    18

    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
    Code:
    setx VAMP_PATH D:
    Last edited by Omegadarkest; May 20th, 2011 at 08:26 AM.

  5. #5
    Join Date
    Nov 2009
    Posts
    6

    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
  •  





Click Here to Expand Forum to Full Width

Featured