2016-01-08 31 views
2

我想清理所有使用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

+0

您可能需要看看 「StandardError的」 代替。 –

回答

2

其最終輸出這不是它如何作品,您不能使用vcvarsall作爲命令,使用devenv ...作爲參數。

由於ProcessStartInfo.FileName使用 「cmd.exe的」,或者更好的

Environment.ExpandEnvironmentVariables("%comspec%") 

由於ProcessStartInfo.Arguments使用:

@"/c <...>\vcvarsall.bat && devenv.com <...>" 

<...>這些片段(目錄,參數等),你已經有了在你的代碼中。

請注意,您應該明確使用devenv.com(無窗口命令行版本),以確保不使用devenv.exe(正常應用程序)。

爲了使事情更健壯,你應該確保你的路徑被正確引用。

下面是完整的例子:

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); 
+0

我嘗試了你所提出的修改,但仍然沒有執行我的bin&obj仍然包含編譯器生成的文件 – rupinder18

+0

當你在命令提示符窗口中運行命令(由'FileName'和'Arguments'構建時)會發生什麼? –

+0

實際上,我正在從控制器內部的web應用程序調用CleanSolution(),我只是收到了我的成功消息 - 「清潔成功」。我的CleanSolution()是在我的服務中定義的。 – rupinder18

相關問題