none
I need to run a command on visual studio command prompt from C# RRS feed

  • Question

  • I have been trying to open Visual Studio command prompt using C# and then run a command on that. I have tried a lot of ways.

    I tried running vcvarsall.bat but the command is being executed on normal cmd only.

    Friday, March 2, 2018 5:01 AM

All replies

  • I have been trying to open Visual Studio command prompt using C# and then run a command on that.

    I tried running vcvarsall.bat but the command is being executed on normal cmd only.

    >I have been trying to open Visual Studio command prompt using C#

    I think you need to be a little clearer. Are you trying to open the
    command prompt at run time from inside your C# program?

    >and then run a command on that.

    Again, please clarify. Do you mean you want your C# program to run a
    command in the command prompt window? If so, run what exactly?

    Or do you mean you want to manually run something in the command prompt
    window that you open from your C# program?

    >I tried running vcvarsall.bat

    AFAIK that sets the environment variables needed for a C/C++ build
    from the command line prompt. I don't see how that would relate to
    your C# program.

    >but the command is being executed on normal cmd only.

    What *exactly* do you mean by "normal cmd only"? What are you expecting
    and trying to do that is not "normal"?

    If you want to open the command prompt manually, in VS2017 Community
    Edition you can use the Tools menu.

    - Wayne

    Friday, March 2, 2018 6:16 AM
  • Thanks for replying.

    So, I need to run a test on visual studio command prompt 2013, I can do this manually but I want to run this from C#.

    I don't want to run it manually, there is one command which I want to run on the vs command prompt 2013.

    So the flow will be like:

    1. Open VS Command Prompt 2013 

    2. Run the Command

    3. Come back to code



    Friday, March 2, 2018 6:31 AM
  • Here's an example of opening a command prompt window and executing the
    command: dir /s /p

    using System.Diagnostics;
    
    ...
    
    Process.Start("cmd", "/K dir /s /p");
    

    - Wayne

    Friday, March 2, 2018 6:33 AM
  • So I need to mention vcvarsall.bat path in dir ?
    Friday, March 2, 2018 6:35 AM
  • Hi,

    I'm afraid what you mentioned is still not clear.
    Please provide the line you input manually. That will be helpful to understand what you want to achieve.

    Regards,

    Ashidacchi

    Friday, March 2, 2018 6:37 AM
  • I just want to run a command on Visual studio command prompt instead of cmd.

    The test runs only on VS 2013 cmd.

    I search on internet I found that we need to run this command on cmd to open vs 2013 cmd

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat
    Friday, March 2, 2018 6:40 AM
  • Hi,

    Thank you for providing input text.
    According to its file extension (.bat), it is batch file and you can write code with C# like this.
    Process.Start("C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat");
    Regards,

    Ashidacchi

    P.S.
      Please refer to the following article:  Process.Start Method (String)
      https://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx
    • Edited by Ashidacchi Friday, March 2, 2018 6:50 AM
    Friday, March 2, 2018 6:46 AM
  • I just want to run a command on Visual studio command prompt instead of cmd.

    The test runs only on VS 2013 cmd.

    I search on internet I found that we need to run this command on cmd to open vs 2013 cmd

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat

    >I just want to run a command on Visual studio command prompt instead
    >of cmd.

    The "Visual studio command prompt" *is* cmd.exe - that's the command
    prompt shell in Win32/64. The only thing that it does differently than
    just opening cmd.exe is that it sets some environment variables used
    for Visual Studio builds from the command line.

    Those environment variables will only be in effect for the lifetime of
    that open process - when that command line window is closed those
    settings will disappear. That's normal behaviour when running a shell
    environment.

    >I search on internet I found that we need to run this command on cmd
    >to open vs 2013 cmd

    >C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat

    As I said before, and as you can see from the VC in the path, that bat
    file just sets the environment variables needed to do a C/C++ build
    from *that* open command prompt window. Once that window closes those
    settings disappear.

    - Wayne

    Friday, March 2, 2018 6:52 AM
  • I just want to run a command on Visual studio command prompt instead of cmd.

    I search on internet I found that we need to run this command on cmd to open vs 2013 cmd

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat

    You still haven't made clear *why* you want to set those environment
    variables. Are you trying to open the VS command prompt window with
    the environment variables set to allow a C/C++ build, and then to
    automatically do a build in that window? Is your C# program supposed
    to do a command line build of a C/C++ program?

    - Wayne

    Friday, March 2, 2018 7:01 AM
  • Yes, the program which I need to run will work only after setting the environment variables.
    Friday, March 2, 2018 7:26 AM
  • Thank you !

    Friday, March 2, 2018 7:27 AM
  • According to its file extension (.bat), it is batch file and you can write code with C# like this.
    Process.Start("C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat");

    As written that will cause compile and intellisense errors. The backslash
    is the escape character, so it has to be doubled

    Process.Start("C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat");
    

    or use a verbatim string:

    Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat");
    

    - Wayne

    Friday, March 2, 2018 8:00 AM
  • I am trying to run 2 commands consecutively but the first one is not being executed.

    string cmdText = "/C \"C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat\" && SECONDCOMMAND ";
                    
    ProcessStartInfo proc = new ProcessStartInfo();
                    proc.FileName = "cmd.exe";
                    proc.UseShellExecute = true;
                    proc.Arguments = cmdText;
                    //proc.RedirectStandardOutput = true;
                    Process test = new Process();
                    test.StartInfo = proc;
                    test.Start();
                    test.WaitForExit();
    Friday, March 2, 2018 8:07 AM
  • Did you include necessary class?
    using System;
    using System.Diagnostics;
    using System.ComponentModel;

    Ashidacchi

    Friday, March 2, 2018 8:08 AM
  • Yes.

    There is no compilation error, it's just the first command is not executed and directly second is getting executed.

    Friday, March 2, 2018 8:11 AM
  • Okay I found the error, after running the batch file on manually opening cmd my SECONDCOMMAND is running but the when I open cmd through C# my command is not working.
    Friday, March 2, 2018 8:22 AM
  • Okay I found the error, after running the batch file on manually opening cmd my SECONDCOMMAND is running but the when I open cmd through C# my command is not working.


    It probably depends on what your second command is. Try this:

    Process.Start("cmd", "/K \"C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat\" && dir");
    

    It should open a command prompt window - the /K should keep it open - and
    then run vcvarsall.bat followed by a dir command. If you do a "set" command
    manually from the prompt you should see the environment variables have
    been set. So both operations should have succeeded.

    - Wayne


    Friday, March 2, 2018 8:47 AM
  • What is a "Visual Studio command prompt?"

    Never heard of that. 

    Be aware that if you start a program, using Visual Studio, you start a normal executable in the Debug folder. 


    Success
    Cor

    Friday, March 2, 2018 9:07 AM
  • What is a "Visual Studio command prompt?"

    Never heard of that. 


    - Wayne

    Friday, March 2, 2018 9:12 AM
  • Wayne,

    I don't have that one, what does it do?


    Success
    Cor

    Friday, March 2, 2018 9:37 AM
  • Hi WayneAKing,

    If you want some suggestions, you need to explain the issue in the way where others can understand.

    I cannot find the image you inserted.
      This is mine (Visual Studio 2017 Pro):
        
    You should provide more details about your environment (more clearly, more exactly).

    Regards,


    Ashidacchi


    • Edited by Ashidacchi Friday, March 2, 2018 10:27 AM
    Friday, March 2, 2018 10:17 AM
  • Wayne,

    I don't have that one, what does it do?



    This one is from VS2017 Community. It's from Tools->External Tools.
    I think the pic shows what it does.

    - Wayne

    Friday, March 2, 2018 2:27 PM
  • Hi WayneAKing,

    If you want some suggestions, you need to explain the issue in the way where others can understand.

     

    Where did you get the idea I "want some suggestions"? I was replying to
    Cor's question. I'm just fine, thank you.

    - Wayne

    Friday, March 2, 2018 2:31 PM
  • Wayne,

    I don't have that one, what does it do?

    Some additional details:

    Developer Command Prompt for Visual Studio
    https://docs.microsoft.com/en-us/dotnet/framework/tools/developer-command-prompt-for-vs

    Running command prompt from inside Visual Studio
    https://docs.microsoft.com/en-us/dotnet/framework/tools/developer-command-prompt-for-vs#running-command-prompt-from-inside-visual-studio

    - Wayne

    Friday, March 2, 2018 2:51 PM
  • Wayne,

    One of those commands which came in Visual Studio from which I see more and more messages telling. 

    "Let them solve bugs instead of adding all kind of not needed features nobody is waiting for"

    Lately new users don't understands anymore what is Visual Studio and what not. 

    Just my opinion of course and thanks for your reply.


    Success
    Cor


    Friday, March 2, 2018 4:18 PM
  • Wayne,

    One of those commands which came in Visual Studio from which I see more and more messages telling. 

    "Let them solve bugs instead of adding all kind of not needed features nobody is waiting for"


    In this case, I'd say it's more a case of keeping a legacy feature.
    It was present as far back as at least Visual C++ 2008, and back then
    was even installed automatically in the Tools menu for the Express
    Edition of VC++ and the Win XP Start menu.

    - Wayne

    Friday, March 2, 2018 8:09 PM
  • Yeah I think it has always existed (except as a different name) in VS since the first version and even in the Microsoft IDE before VS.


    Sam Hobbs
    SimpleSamples.Info

    Friday, March 2, 2018 9:53 PM
  • I think you have at least two options.

    You could generate a batch file (with a "bat" or "cmd" extension) in a temporary location and then execute that and delete it after you are done or you could create such a file to exist with your application.

    Or you could use redirection to send commands to a cmd.exe window and get the results back. The Process class supports redirection of standard input and standard output.

    I think either of those are guaranteed to work.



    Sam Hobbs
    SimpleSamples.Info

    Friday, March 2, 2018 9:57 PM
  • See what happens with 

    procInfo.Arguments = "/k \"C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/Common7/Tools/VsDevCmd.bat\" && mstest /help";

    PS: you may have diffirent visual studio so dont forget to change the path

    Wednesday, September 19, 2018 3:34 PM
  • See what happens with 

    procInfo.Arguments = "/k \"C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/Common7/Tools/VsDevCmd.bat\" && mstest /help";


    Since this thread was started and then went dormant six months ago, I hope
    the OP isn't still struggling with it.

    - Wayne

    Wednesday, September 19, 2018 7:55 PM