basyura's blog

あしたになったらほんきだす。

C# - 任意の sln をビルドする

あってるのか良くわからないけど、ビルドはできた。

public void Build(string slnPath)
{
    FileInfo info = new FileInfo(slnPath);
    
    Directory.SetCurrentDirectory(info.DirectoryName);

    ProcessStartInfo psInfo = new ProcessStartInfo()
    {
        FileName  = "xbuild",
        Arguments = info.Name,
        // コンソール・ウィンドウを開かない
        CreateNoWindow  = true,
        // シェル機能を使用しない
        UseShellExecute = false,
        // 標準出力をリダイレクト
        RedirectStandardOutput = true,
    };
    // アプリの実行開始
    Process process = Process.Start(psInfo);

    using (var reader = process.StandardOutput) 
    {
        while(reader.Peek() >= 0)
        {
            var line = reader.ReadLine();
            Console.WriteLine(line);
        }
    }
}