如何在c#代码中将参数传递给命令提示符?

时间:2016-06-16 13:11:04

标签: c# batch-file cmd msbuild

使用下面的代码,我只能启动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);

4 个答案:

答案 0 :(得分:1)

我带来了这个解决方法:

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文件作为Process启动。

答案 1 :(得分:0)

  

C:\ Program'未被识别为内部或外部命令,   可操作程序或批处理文件。

您收到此错误,因为您的路径中有ProgramFile

之间的空格

尝试在路径的开头和结尾放置双引号

答案 2 :(得分:0)

尝试通过

将目录更改为“C:\ Program Files(x86)\ Microsoft Visual Studio 9.0 \ VC \”,而不是传递可执行文件的整个路径。
oInfo.WorkingDirectory = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\";  

然后只需调用vcvarsass.bat

希望有帮助

答案 3 :(得分:0)

使用Arguments

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

就像在这个例子中一样:Using cmd.exe from C#