786 Stimmen

Eingabeaufforderungsbefehle ausführen

Gibt es eine Möglichkeit, Eingabeaufforderung Befehle aus einer C#-Anwendung ausführen? Wenn ja, wie würde ich das folgende tun:

copy /b Image1.jpg + Archive.rar Image2.jpg

Dies bettet im Grunde eine RAR-Datei in ein JPG-Bild ein. Ich habe mich gefragt, ob es eine Möglichkeit gibt, dies automatisch in C# zu tun.

13voto

Uniquedesign Punkte 469

Wenn Sie das cmd-Fenster geöffnet lassen oder es in Winform/Wpf verwenden möchten, dann verwenden Sie es wie folgt

    string strCmdText;
//For Testing
    strCmdText= "/K ipconfig";

 System.Diagnostics.Process.Start("CMD.exe",strCmdText);

/K

Wird das cmd-Fenster offen halten

12voto

Tyrrrz Punkte 2145

Sie können dies tun mit CliWrap in einer Zeile:

var result = await Cli.Wrap("cmd")
    .WithArguments("copy /b Image1.jpg + Archive.rar Image2.jpg")
    .ExecuteBufferedAsync();

var stdOut = result.StandardOutput;

11voto

Elad Punkte 1363

Wenn Sie den Befehl im asynchronen Modus ausführen und die Ergebnisse ausdrucken möchten, können Sie diese Klasse verwenden:

    public static class ExecuteCmd
{
    /// <summary>
    /// Executes a shell command synchronously.
    /// </summary>
    /// <param name="command">string command</param>
    /// <returns>string, as output of the command.</returns>
    public static 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", "/c " + 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 = true;
            // 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
            Console.WriteLine("ExecuteCommandSync failed" + objException.Message);
        }
    }

    /// <summary>
    /// Execute the command Asynchronously.
    /// </summary>
    /// <param name="command">string command.</param>
    public static void ExecuteCommandAsync(string command)
    {
        try
        {
            //Asynchronously start the Thread to process the Execute command request.
            Thread objThread = new 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 )
        {
            // Log the exception
        }
        catch (ThreadAbortException )
        {
            // Log the exception
        }
        catch (Exception )
        {
            // Log the exception
        }
    }

}

10voto

Verax Punkte 2187

Dies kann auch geschehen durch P/Aufruf der C-Standardbibliothek system Funktion.

using System.Runtime.InteropServices;

[DllImport("msvcrt.dll")]
public static extern int system(string format);

system("copy Test.txt Test2.txt");

Ausgabe:

      1 file(s) copied.

8voto

Slai Punkte 20907

Mit einem Verweis auf Microsoft.VisualBasic

Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X