使用下面的代碼我只能啓動Visual Studio 2008命令提示符,但現在我必須將網站發佈到本地驅動器上的目標路徑。如何將參數傳遞給c#代碼中的命令提示符?
我需要的C#代碼,做類似如下的命令:
的MSBuild /目標:構建/ P:BuildingProject = TRUE; OUTDIR = C:\ TEMP \編譯\ ccosapp.sln我第一次嘗試(失敗 - 什麼都不做):
下面是我試過的代碼,它不會做任何事情:
ProcessStartInfo oInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat", @"msbuild C:\Users\Johan\Documents\Visual Studio 2008\Projects\PRJAPP\PRJAPP.sln");
oInfo.UseShellExecute = false;
oInfo.ErrorDialog = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(oInfo);
System.IO.StreamReader oReader2 = p.StandardOutput;
string sRes = oReader2.ReadToEnd();
oReader2.Close();
的另一種嘗試(也FAI LS):
string strCmdText = "/k \"C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\vcvarsall.bat\" && msbuild /p:OutDir=C:\\Temp\\build \"C:\\Users\\Johan\\Documents\\Visual Studio 2008\\Projects\\CCOSApp\\ccosapp.sln\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
我得到的錯誤在我的CMD窗口:
'C:\ PROGRAM' 不被識別爲一個內部或外部命令,可操作 程序或批處理文件。
的最後一次嘗試(失敗以及 - 得到同樣的錯誤在我的第2次嘗試):
/// <span class="code-SummaryComment"><summary></span>
/// Executes a shell command synchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command</param></span>
/// <span class="code-SummaryComment"><returns>string, as output of the command.</returns></span>
public void ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/k " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = false;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
}
/// <span class="code-SummaryComment"><summary></span>
/// Execute the command Asynchronously.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="command">string command.</param></span>
public void ExecuteCommandAsync(string command)
{
try
{
//Asynchronously start the Thread to process the Execute command request.
System.Threading.Thread objThread = new System.Threading.Thread(new ParameterizedThreadStart(ExecuteCommandSync));
//Make the thread as background thread.
objThread.IsBackground = true;
//Set the Priority of the thread.
objThread.Priority = ThreadPriority.AboveNormal;
//Start the thread.
objThread.Start(command);
}
catch (ThreadStartException objException)
{
// Log the exception
}
catch (ThreadAbortException objException)
{
// Log the exception
}
catch (Exception objException)
{
// Log the exception
}
}
string cmdStr = @" ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat""
cd ""C:\Users\Johan\Documents\Visual Studio 2008\Projects\CCOSApp""
msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build
ccosapp.sln";
new VsFunctions().ExecuteCommandAsync(cmdStr);
'ProcessStartInfo'具有'Arguments'屬性。 – Crowcoder