Führen Sie eine Exe aus dem C # -Code aus


163

Ich habe eine exe-Dateireferenz in meinem C # -Projekt. Wie rufe ich diese Exe aus meinem Code auf?

Antworten:


286
using System.Diagnostics;

class Program
{
    static void Main()
    {
        Process.Start("C:\\");
    }
}

Wenn Ihre Anwendung cmd-Argumente benötigt, verwenden Sie Folgendes:

using System.Diagnostics;

class Program
{
    static void Main()
    {
        LaunchCommandLineApp();
    }

    /// <summary>
    /// Launch the application with some options set.
    /// </summary>
    static void LaunchCommandLineApp()
    {
        // For the example
        const string ex1 = "C:\\";
        const string ex2 = "C:\\Dir";

        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "dcm2jpg.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
             // Log error.
        }
    }
}

1
startInfo.UseShellExecute = falsewar eine tolle Sache ... Es hat bei mir wie ein Zauber funktioniert! Danke dir! :)
RisingHerc

@ logganB.lehman-Prozess hängt für immer an exeProcess.WaitForExit (); irgendeine Idee?
Dragon


11

Beispiel:

System.Diagnostics.Process.Start("mspaint.exe");

Code kompilieren

Kopieren Sie den Code und fügen Sie ihn in die Main- Methode einer Konsolenanwendung ein. Ersetzen Sie "mspaint.exe" durch den Pfad zu der Anwendung, die Sie ausführen möchten.


15
Wie bietet dies mehr Wert als die bereits erstellten Antworten? Die akzeptierte Antwort zeigt auch die Verwendung vonProcess.Start()
Standard

3
SO - es ist in Ordnung, einem Anfänger mit vereinfachten, schrittweisen Beispielen zu helfen, bei denen viele Details entfernt wurden. Auch ok, um Kappen zu verwenden: P
DukeDidntNukeEm

Ich brauchte nur einen schnellen Weg, um die Exe auszuführen, und das war wirklich hilfreich. Vielen Dank :)
Sushant Poojary

7

Beispiel:

Process process = Process.Start(@"Data\myApp.exe");
int id = process.Id;
Process tempProc = Process.GetProcessById(id);
this.Visible = false;
tempProc.WaitForExit();
this.Visible = true;

Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.