2017-06-06 69 views
0

我想以編程方式運行visual studio命令。我嘗試了上述代碼,但沒有任何幫助。我得到的是打開了我的項目目錄的命令提示符。無法打開Visual Studio命令提示符實用地使用VS 2015

我已經使用Execute(「VS140COMNTOOLS」)作爲輸入。

private void Execute(string vsEnvVar) { 

     var vsInstallPath = Environment.GetEnvironmentVariable(vsEnvVar); 

     if (Directory.Exists(vsInstallPath)) { 
      var filePath = vsInstallPath + "vsvars32.bat"; 
      if (File.Exists(filePath)) { 
       //start vs command process 
       Process proc = new Process(); 

       var command = Environment.GetEnvironmentVariable("ComSpec"); 
       command = @"" + command + @""; 

       //var batfile = @"E:\Test\vstest.bat"; 
       var args = string.Format("/S/K \" \"{0}\" \"", filePath); 

       proc.StartInfo.FileName = command; 
       proc.StartInfo.Arguments = args; 

       //proc.StartInfo.RedirectStandardInput = true; 
       //proc.StartInfo.RedirectStandardOutput = true; 
       proc.StartInfo.CreateNoWindow = false; 
       proc.StartInfo.UseShellExecute = false; 

       proc.Start(); 
      } else { 
       Console.WriteLine("File Does not exists " + filePath); 
      } 
     } 
    } 
+0

private Process Execute(string vsEnvVar) { Process process = new Process(); ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");//assume location is in path. Otherwise use ComSpec env variable psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo = psi; // attach output events process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived); process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived); process.StartInfo = psi; process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.StandardInput.WriteLine(string.Format("call \"%{0}%vsvars32.bat\""), vsEnvVar); process.StandardInput.Flush(); return process; } 

現在,你可以執行任何命令,爲什麼2手輪報價與他們之間的空間的批處理文件? – BugFinder

回答

0

試試這個:通過寫process.StandardInput

process.StandardInput.WriteLine(@"msbuild c:\MySolution.sln /t:Clean"); 
+0

感謝您的答覆。我試過你的代碼,但仍然無法獲得所需的結果 –

+0

我得到的只是一個命令提示符打開,我無法添加任何東西。 –

+0

我修改了一下你的代碼,我試圖看到命令提示符窗口 –

相關問題