■ 인증서를 설치하는 방법을 보여준다.
▶ 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 70 71 72 73 74 75 76 77 78 79 |
using System; using System.IO; using System.Security.Cryptography.X509Certificates; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> private static void Main(string[] argumentArray) { if(argumentArray == null || argumentArray.Length < 1) { Console.WriteLine("프로그램 인자로 인증서 파일(.cer) 경로를 지정해 주시기 바랍니다."); return; } string filePath = argumentArray[0]; if(!File.Exists(filePath)) { Console.WriteLine("지정한 파일 경로에 파일이 존재하지 않습니다."); return; } try { InstallCertificate(StoreLocation.LocalMachine, StoreName.Root , filePath); InstallCertificate(StoreLocation.LocalMachine, StoreName.TrustedPublisher, filePath); Console.WriteLine("인증서가 설치되었습니다 : {0}", filePath); } catch(Exception exception) { Console.WriteLine("인증서 설치시 에러가 발생했습니다."); Console.WriteLine(exception.ToString()); } } #endregion #region 인증서 설치하기 - InstallCertificate(storeLocation, storeName, filePath) /// <summary> /// 인증서 설치하기 /// </summary> /// <param name="storeLocation">저장 위치</param> /// <param name="storeName">저장명</param> /// <param name="filePath">파일 경로</param> private static void InstallCertificate(StoreLocation storeLocation, StoreName storeName, string filePath) { X509Store store = new X509Store(storeName, storeLocation); store.Open(OpenFlags.ReadWrite); store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(filePath))); store.Close(); } #endregion } } |
※ 관리자 권한으로 실행해야 한다.