[C#] C # 코드에서 exe 실행

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



답변

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.
        }
    }
}


답변


답변

예:

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

코드 컴파일

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


답변

예:

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


답변

나는 이것이 잘 대답 된 것을 알고 있지만, 관심이 있다면 명령을 훨씬 쉽게 실행하는 라이브러리를 작성했습니다.

https://github.com/twitchax/Sheller 에서 확인 하십시오 .


답변