我想清理所有使用c#的代碼解決方案中的項目。我使用devenv clean commnad,我的方法正常執行,沒有任何異常,但是沒有對解決方案執行任何操作。我的解決方案中的所有項目的bin和obj文件夾仍包含編譯器生成的文件。請讓我知道問題出在哪裏。使用devenv編程式地清理解決方案
C#代碼
public string CleanSolution(BO.Project project)
{
try
{
if (!File.Exists(project.SolutionFilePath)) return "Solution file not found";
var cleanCommand = "Devenv " + project.SolutionFilePath + " /Clean";
//var cleanProcess = Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe", cleanCommand);
//if (cleanProcess != null) cleanProcess.WaitForExit();
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = Environment.ExpandEnvironmentVariables("%comspec%");
startInfo.Arguments = string.Format(@"/c ""c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" && devenv.com ""{0}"" /Clean",project.SolutionFilePath);
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.Start();
// Read the error stream first and then wait.
string error = process.StandardError.ReadToEnd();
StringBuilder q = new StringBuilder();
while (!process.HasExited)
{
q.Append(process.StandardOutput.ReadToEnd());
}
string r = q.ToString();
var message = "Clean Succeeded";
IsClean = true;
return message;
}
catch (Exception e)
{
return string.Format("Message: {0}", e.Message) + Environment.NewLine +
string.Format("Stack Tracke: {0}{1}", Environment.NewLine, e.StackTrace) + Environment.NewLine +
(e.InnerException != null
? string.Format("Inner Exception: {0}", e.InnerException.Message) + Environment.NewLine
: string.Empty) + Environment.NewLine +
(e.InnerException != null
? string.Format("Inner Exception Stack Tracke: {0}", e.InnerException.StackTrace) +
Environment.NewLine
: string.Empty) + Environment.NewLine;
}
}
我試圖重定向過程的輸出,以便看看會發生什麼,但我得到空字符串作爲變量processOutput
您可能需要看看 「StandardError的」 代替。 –