0

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

Rob D
  • 71
  • 1
  • 6

2 Answers2

1

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"""
    };
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Great! Now I also understand @Mark comment:) I will not close this thread yet as maybe someone will come up with cleaner then mine solution. But yours fixes my problem:) – Rob D May 10 '20 at 09:48
  • What part of toolset dou you actually need? What are you trying to achieve? – Guru Stron May 10 '20 at 09:51
  • I would definitely like to compile C project with `msbuild`, but also recently I found out that I need a way to dissassembly program programitically and so I thought I would like to use `dumpbin` as well. – Rob D May 10 '20 at 09:53
  • @RobD, have you thought about using [Roslyn](https://stackoverflow.com/a/35745154/2501279)? – Guru Stron May 10 '20 at 10:38
0

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.

Rob D
  • 71
  • 1
  • 6