using System.Diagnostics;
using System.Text;
using System.Xml;
<summary>
</summary>
private const string MSBuildPath = @"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe";
<summary>
</summary>
<param name="projectFilePath"></param>
<param name="outputDirPath"></param>
<param name="isDebug"></param>
<param name="outputDataReceivedEventHandler"></param>
<param name="errorDataReceivedEventHandler"></param>
<returns></returns>
private int ExecuteMSBuild(string projectFilePath,
string outputDirPath,
bool isDebug,
DataReceivedEventHandler outputDataReceivedEventHandler,
DataReceivedEventHandler errorDataReceivedEventHandler)
{
using (Process p = new Process())
{
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = true;
p.StartInfo.FileName = MSBuildPath;
StringBuilder args = new StringBuilder();
args.AppendFormat("\"{0}\"", projectFilePath);
args.AppendFormat(" /p:Configuration={0}", isDebug ? "Debug" : "Release");
args.AppendFormat(" /p:OutputPath=\"{0}\"", outputDirPath);
args.Append(" /p:OptionExplicit=true");
args.Append(" /p:OptionCompare=binary");
args.Append(" /p:OptionStrict=true");
args.Append(" /p:OptionInfer=true");
args.Append(" /p:BuildProjectReferences=false");
args.AppendFormat(" /p:DebugType={0}", isDebug ? "full" : "none");
args.AppendFormat(" /p:DebugSymbols={0}", isDebug ? "true" : "false");
args.AppendFormat(" /p:DocumentationFile={0}", isDebug ? string.Format("{0}.xml", GetAssemblyName(projectFilePath)) : " ");
args.Append(" /t:Clean;Rebuild");
p.StartInfo.Arguments = args.ToString();
if (outputDataReceivedEventHandler != null || errorDataReceivedEventHandler != null)
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (outputDataReceivedEventHandler != null)
{
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += outputDataReceivedEventHandler;
}
if (errorDataReceivedEventHandler != null)
{
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += errorDataReceivedEventHandler;
}
}
p.Start();
if (outputDataReceivedEventHandler != null)
p.BeginOutputReadLine();
if (errorDataReceivedEventHandler != null)
p.BeginErrorReadLine();
p.WaitForExit();
return p.ExitCode;
}
}
<summary>
</summary>
<param name="projectFilePath"></param>
<returns></returns>
private static string GetAssemblyName(string projectFilePath)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(projectFilePath);
XmlNamespaceManager xnm = new XmlNamespaceManager(xdoc.NameTable);
xnm.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode node = xdoc.SelectSingleNode("/ns:Project/ns:PropertyGroup/ns:AssemblyName", xnm);
if (node != null)
return node.InnerText;
else
return "";
}