C # 코드에서 exe 실행


163

내 C # 프로젝트에 exe 파일 참조가 있습니다. 내 코드에서 exe를 어떻게 호출합니까?

답변:


287
using System.Diagnostics;

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

응용 프로그램에 cmd 인수가 필요한 경우 다음과 같이 사용하십시오.

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 = false굉장한 일이었다. .. 그것은 매력처럼 나를 위해 일했다! 감사합니다! :)
RisingHerc

@ logganB.lehman 프로세스는 exeProcess.WaitForExit ()에서 영원히 중단됩니다. 어떤 생각?
드래곤


11

예:

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

코드 컴파일

코드를 복사하여 콘솔 응용 프로그램 의 Main 메서드에 붙여 넣습니다 . "mspaint.exe"를 실행하려는 응용 프로그램의 경로로 바꿉니다.


15
이것이 이미 작성된 답변보다 더 많은 가치를 제공하는 방법은 무엇입니까? 수락 된 답변은 다음과 같은 사용법을 보여줍니다.Process.Start()
기본

3
따라서 많은 세부 정보가 제거 된 단계별 예제를 단순화하여 초보자를 돕는 것이 좋습니다. 또한 모자를 사용해도 좋습니다 : P
DukeDidntNukeEm

exe를 실행하는 빠른 방법이 필요했으며 이것이 실제로 도움이되었습니다. :) 주셔서 감사합니다
Sushant Poojary에게

7

예:

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

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.