Rob D
Rob D

Reputation: 111

C# DeveloperCommandPrompt

I want to call commands such as msbuild/dumpbin with Environment Variables which are set in DevelopersCommandPrompt.

I have no experience with C#, and I couldn't find a clean way to achieve this. What I thought of, was to first run Process DevelopersCommandPrompt and read set command output to retrieve all process environment variables and then call new Process, but this time with the dictionary of retrieved env variables.

But it turns out I cannot even get the set command result to work.

class Program
{
    static void Main(string[] args)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo {
            FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\Tools\\VsDevCmd.bat",
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true
    };

        Process devCmd = new Process();
        devCmd.StartInfo = startInfo;
        devCmd.Start();

        devCmd.StandardInput.WriteLine("set");
        devCmd.StandardInput.Flush();
        devCmd.StandardInput.Close();
        devCmd.WaitForExit();
        Console.WriteLine(devCmd.StandardOutput.ReadToEnd());
    }
}

I believe I'm doing something wrong (as when I change set to dir it doesn't work too). Unfortunately I have no clue what am I missing:)

Moreover if anyone has a smarter/cleaner solution for running Microsoft toolset from C# then I would love to hear it. :)

Thanks

Upvotes: 0

Views: 151

Answers (2)

Rob D
Rob D

Reputation: 111

Update I'm closing the thread.

@Guru Stron has found why I couldn't gather env variables from Developer Command Prompt. You can check his answer above, but it is just a bug in my code.

Compiling If anyone is interested in just compiling c++ visual studio project from c# then I've found a solution here: https://gist.github.com/jeremybeavon/5736e0acbf3729092887

Make sure you add as references correct (not 4.0 version) of all Microsoft.Msbuild.* packages.

If you are interested in compiling c# project instead then I would recommend using https://github.com/daveaglick/Buildalyzer project.

Upvotes: 0

Guru Stron
Guru Stron

Reputation: 142233

You need to use cmd.exe as FileName and set Arguments to path to VS dev console bat file like this:

ProcessStartInfo startInfo = new ProcessStartInfo
    {
        FileName = @"cmd.exe",
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        Arguments = @"""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"""
    };

Upvotes: 1

Related Questions