■ Process 클래스를 사용해 윈도우즈 서비스를 실행하는 방법을 보여준다.
▶ WindowsServiceHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
using System.Diagnostics; namespace TestProject { /// <summary> /// 윈도우즈 서비스 헬퍼 /// </summary> public static class WindowsServiceHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 윈도우즈 서비스 생성하기 - CreateWindowsService(serviceName, argumentList) /// <summary> /// 윈도우즈 서비스 생성하기 /// </summary> /// <param name="serviceName">서비스명</param> /// <param name="argumentList">인자 리스트</param> /// <returns>처리 결과</returns> public static int CreateWindowsService(string serviceName, string argumentList) { Process process = GetProcess("sc.exe", $"create {serviceName} binPath= \"{argumentList}\""); process.Start(); process.WaitForExit(); return process.ExitCode; } #endregion #region 윈도우즈 서비스 시작하기 - StartWindowsService(serviceName) /// <summary> /// 윈도우즈 서비스 시작하기 /// </summary> /// <param name="serviceName">서비스명</param> /// <returns>처리 결과</returns> public static int StartWindowsService(string serviceName) { Process process = GetProcess("sc.exe", $"start {serviceName}"); process.Start(); process.WaitForExit(); return process.ExitCode; } #endregion #region 윈도우즈 서비스 중단하기 - StopWindowsService(serviceName) /// <summary> /// 윈도우즈 서비스 중단하기 /// </summary> /// <param name="serviceName">서비스명</param> /// <returns>처리 결과</returns> public static int StopWindowsService(string serviceName) { Process process = GetProcess("sc.exe", $"stop {serviceName}"); process.Start(); process.WaitForExit(); return process.ExitCode; } #endregion #region 윈도우즈 서비스 제거하기 - DeleteWindowsService(serviceName) /// <summary> /// 윈도우즈 서비스 제거하기 /// </summary> /// <param name="serviceName">서비스명</param> /// <returns>처리 결과</returns> public static int DeleteWindowsService(string serviceName) { Process process = GetProcess("sc.exe", $"delete {serviceName}"); process.Start(); process.WaitForExit(); return process.ExitCode; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 프로세스 구하기 - GetProcess(filePath, argumentList) /// <summary> /// 프로세스 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <param name="argumentList">인자 리스트</param> /// <returns>프로세스</returns> private static Process GetProcess(string filePath, string argumentList) { Process process = new Process(); process.StartInfo.UseShellExecute = true; process.StartInfo.FileName = filePath; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.Verb = "runas"; process.StartInfo.Arguments = argumentList; return process; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main(string[] argumentArray) { string serviceName = "nats01"; string argumentList = @"d:\nats\bin\gnatsd.exe --config d:\nats\config\server.conf"; int exitCode; exitCode = WindowsServiceHelper.CreateWindowsService(serviceName, argumentList); if(exitCode != 0) { Console.WriteLine("윈도우즈 서비스 생성을 실패했습니다 : {0}", exitCode); return; } exitCode = WindowsServiceHelper.StartWindowsService(serviceName); if(exitCode != 0) { Console.WriteLine("윈도우즈 서비스 시작을 실패했습니다 : {0}", exitCode); return; } exitCode = WindowsServiceHelper.StopWindowsService(serviceName); if(exitCode != 0) { Console.WriteLine("윈도우즈 서비스 중단을 실패했습니다 : {0}", exitCode); return; } exitCode = WindowsServiceHelper.DeleteWindowsService(serviceName); if(exitCode != 0) { Console.WriteLine("윈도우즈 서비스 삭제를 실패했습니다 : {0}", exitCode); return; } Console.WriteLine("작업을 성공적으로 처리했습니다."); } #endregion } } |