簡體   English   中英

如何在C#代碼中將參數傳遞給命令提示符?

[英]How to pass arguments to Command Prompt in c# code?

使用下面的代碼,我只能啟動Visual Studio 2008命令提示符,但是現在,我必須將網站發布到本地驅動器上的目標路徑。

我需要執行以下命令的C#代碼:

msbuild / target:Build / p:BuildingProject = true; OutDir = C:\\ Temp \\ build \\ 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();

另一嘗試(也失敗):

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”識別為內部或外部命令,可操作程序或批處理文件。

最后一次嘗試(也失敗-與我的第二次嘗試得到相同的錯誤):

/// <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);

我提供了這種解決方法:

StreamWriter w = new StreamWriter(@"C:\temp\publish.bat");
w.WriteLine(@"call ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat""");
w.WriteLine(@"call cd ""C:\Users\Johan\Documents\Visual Studio 2008\Projects\CCOSApp""");
w.WriteLine(@"call msbuild /target:Build /p:BuildingProject=true;OutDir=C:\Temp\build ccosapp.sln");
w.Close();

System.Diagnostics.Process proc = new System.Diagnostics.Process();
ProcessStartInfo psi = new ProcessStartInfo(@"publish.bat");
psi.WorkingDirectory = @"C:\temp\";
proc.StartInfo = psi;
proc.Start();
  1. 我創建一個.bat文件。
  2. 我在其中寫命令。
  3. 最后,我將.bat文件作為進程啟動。

C:\\ Program'無法識別為內部或外部命令,可操作程序或批處理文件。

您收到此錯誤的原因是ProgramFile之間的路徑中有空間

嘗試在路徑的開頭和結尾加上雙引號

而不是傳遞可執行文件的整個路徑,而是嘗試通過以下方式將目錄更改為“ C:\\ Program Files(x86)\\ Microsoft Visual Studio 9.0 \\ VC \\”

oInfo.WorkingDirectory = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\";  

然后只需調用vcvarsass.bat

希望有幫助

使用StartInfoArguments屬性時是否StartInfo

Process cmd = new Process();
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat";
cmd.StartInfo.Arguments = @"msbuild C:\Users\Johan\Documents\Visual Studio 2008\Projects\PRJAPP\PRJAPP.sln";
cmd.Start();
cmd.WaitForExit();

即像在此示例中一樣: 從C#使用cmd.exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM