2016-06-16 4 views
0

Mit dem folgenden Code kann ich Visual Studio 2008 Command Prompt nur starten, aber jetzt muss ich eine Website in einem Zielpfad auf meinem lokalen Laufwerk veröffentlichen.Wie übergeben Sie Argumente an die Eingabeaufforderung in C# -Code?

Ich brauche C# -Code, die so etwas wie den folgenden Befehl tut:

msbuild/target: Bauen/p: BuildingProject = true; OutDir = C: \ Temp \ build \ ccosapp.sln Mein erster Versuch (nicht - tut nichts):

Im Folgenden finden Sie den Code habe ich versucht, die nichts tun:

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

Ein weiterer Versuch (auch 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); 

Ich erhalte den Fehler in meinem CMD-Fenster:

'C: \ Program' wird nicht als interner oder externer Befehl erkannt, bedienbare Programm oder Batch-Datei.

Der letzte Versuch (nicht auch - wie in meinem zweiten Versuch, den gleichen Fehler):

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

'ProcessStartInfo' hat eine 'Arguments'-Eigenschaft. – Crowcoder

Antwort

0

Ich kam mit dieser Problemumgehung:

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. Ich erstelle eine .bat-Datei.
  2. Ich schreibe die Befehle darin.
  3. Schließlich starte ich die .bat-Datei als ein Prozess.
0

C: \ Program‘als interner oder externer Befehl nicht erkannt, ausführbares Programm oder Batch-Datei.

Sie bekommen diese Fehler, da es Raum in Ihrem Pfad befindet, zwischen Program und File

Versuch doppelte Anführungszeichen am Anfang und Ende des Weges

0

Statt vorbei den ganzen Pfad platzieren der ausführbaren Datei, versuchen Sie das Verzeichnis zu ändern, um "C: \ Program Files (x86) \ Microsoft Visual Studio 9.0 \ VC \" über

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

dann die VCVA einfach anrufen rsass.bat

Hoffentlich

0

Ist es hilft, wenn die Symbol Arguments Eigenschaft von StartInfo

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

wie das heißt in diesem Beispiel: Using cmd.exe from C#