C # kodundan bir exe çalıştırın


163

C # projemde bir exe dosyası başvurusu var. Bu exe kodumdan nasıl çağırabilirim?

Yanıtlar:


287
using System.Diagnostics;

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

Uygulamanız cmd bağımsız değişkenlerine ihtiyaç duyuyorsa, aşağıdakine benzer bir şey kullanın:

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 = falseharika bir şeydi ... Benim için bir cazibe gibi çalıştı! Teşekkür ederim! :)
RisingHerc

@ logganB.lehman işlemi exeProcess.WaitForExit () üzerinde sonsuza kadar asılı kalır; Herhangi bir fikir?
Ejderha


11

Misal:

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

Kodu Derleme

Kodu kopyalayın ve bir konsol uygulamasının Ana yöntemine yapıştırın . "Mspaint.exe" yerine, çalıştırmak istediğiniz uygulamanın yolunu yazın.


15
Bu, önceden oluşturulmuş cevaplardan nasıl daha fazla değer sağlar? Kabul edilen cevap ayrıca aşağıdakileri de gösterirProcess.Start()
Varsayılan

3
SO - basitleştirilmiş, adım adım örneklerle birçok detayın çıkarıldığı bir acemiye yardımcı olmak sorun değil. Ayrıca kapaklar kullanmak için ok: P
DukeDidntNukeEm

Sadece exe yürütmek için hızlı bir yol gerekli ve bu gerçekten yararlı oldu. Teşekkür ederim :)
Sushant Poojary

7

Misal:

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

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.