0

I am trying to open Visual studio Command prompt using C# code.

Here is my code

private void Execute(string vsEnvVar)
{

 var vsInstallPath = Environment.GetEnvironmentVariable(vsEnvVar);

 // vsEnvVar can be VS100COMNTOOLS, VS120COMNTOOLS, VS140COMNTOOLS

 if (Directory.Exists(vsInstallPath))
 {
    var filePath = vsInstallPath + "vsvars32.bat";
    if (File.Exists(filePath))
    {
        //start vs command process
        Process proc = new Process();

        var command = Environment.GetEnvironmentVariable("ComSpec");
        command = @"" + command + @"";

        var batfile = @"E:\Test\vstest.bat";
        var args = string.Format("/K  \"{0}\" \"{1}\"" ,filePath, batfile);  

        proc.StartInfo.FileName = command;
        proc.StartInfo.Arguments = args;
        proc.StartInfo.CreateNoWindow = false;
        proc.StartInfo.UseShellExecute = false;  

        proc.Start();
     }
     else
     {
        Console.WriteLine("File Does not exists " + filePath);
     }
 }
}

But the args string is not getting properly formatted. I am getting below formatted string

"/K  \"C:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\Tools\\vsvars32.bat\" \"E:\\Test\\vstest.bat\""    

extra "\" is getting added.

Please point out what I am missing. Thanks

2
  • What is the end result of what you are trying to achieve? Commented Jan 22, 2016 at 11:16
  • @MaYaN, I have to run msbuild on solution file, Solution file can be created using vs 10, 12 or 14. Commented Jan 25, 2016 at 8:07

1 Answer 1

1

The string is being formatted as you asked, but you have asked for the wrong thing. "E:\Test\VStest.bat" is being passed as an argument to VCVars.bat, but I suspect you want it to be executed after it.

Try this:

var args = string.Format("/S/K \" \"{0}\" && \"{1}\" \"" ,filePath, batFile);

This should produce:

"/S/K \" \"C:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\Tools\\vsvars32.bat\" && \"E:\\Test\\vstest.bat\" \" \"

Which as a string is:

/S/K " "C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat" && "E:\Test\vstest.bat" "
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for reply, Now able to open visual studio command prompt. But some invalid characters are getting appended in the command F:\Program\Debug >cd /D E:/Temp/Prog. </br> My first command in bat file is change directory to some project solution (.sln) path. But "" is getting appendind in the command. </br> I am using args as <br> var args = string.Format("/K \" \"{0}\" && \"{1}\" \"", filePath, batfile);
Got it working, changed the Encoding of batfile to UTF-8 without BOM.
CMD.exe, the windows command prompt, does not understand UTF-8 by default, you might try chcp 65001 to set the codepage to utf8 (I haven't tested this)
Also you really need the /S argument here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.