■ Folder 인터페이스를 사용해 특정 폴더에서 추가/삭제/폴더명 변경 이벤트를 관리하는 방법을 보여준다.
▶ FolderContainer.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 |
using Microsoft.Office.Interop.Outlook; using System.Collections.Generic; namespace TestProject { /// <summary> /// 폴더 컨테이너 /// </summary> public class FolderContainer { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 부모 - Parent /// <summary> /// 부모 /// </summary> public FolderContainer Parent { get; set; } #endregion #region 소스 키 - SourceKey /// <summary> /// 소스 키 /// </summary> public string SourceKey { get; set; } #endregion #region 소스 폴더 - SourceFolder /// <summary> /// 소스 폴더 /// </summary> public Folder SourceFolder { get; set; } #endregion #region 소스 폴더 컬렉션 - SourceFolders /// <summary> /// 소스 폴더 컬렉션 /// </summary> public Folders SourceFolders { get; set; } #endregion #region 자식 리스트 - ChildList /// <summary> /// 자식 리스트 /// </summary> public List<FolderContainer> ChildList { get; set; } = new List<FolderContainer>(); #endregion } } |
▶ CustomAddIn.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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
using Microsoft.Office.Interop.Outlook; using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows.Forms; namespace TestProject { /// <summary> /// 커스텀 애드인 /// </summary> public partial class CustomAddIn { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 폴더 컨테이너 딕셔너리 /// </summary> private Dictionary<string, FolderContainer> folderContainerDictionary = new Dictionary<string, FolderContainer>(); /// <summary> /// 필터 폴더명 /// </summary> private readonly string filterFolderName = "Sales Force"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e) /// <summary> /// 커스텀 애드인 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Startup(object sender, EventArgs e) { BuildFolderContainerDictionary(); } #endregion #region 커스텀 애드인 셧다운시 처리하기 - CustomAddIn_Shutdown(sender, e) /// <summary> /// 커스텀 애드인 셧다운시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Shutdown(object sender, EventArgs e) { } #endregion #region 폴더 변경시 처리하기 - folders_FolderChange(folder) /// <summary> /// 폴더 변경시 처리하기 /// </summary> /// <param name="folder">폴더</param> private void folders_FolderChange(MAPIFolder Folder) { Debug.WriteLine("BEGIN CHANGE FOLDER"); try { ClearFolderContainerDictionaryData(); BuildFolderContainerDictionary(); } catch(System.Exception exception) { Debug.WriteLine(exception.ToString()); Debug.WriteLine("STOP CHANGE FOLDER"); return; } Debug.WriteLine("END CHANGE FOLDER"); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region VSTO에서 생성한 코드 /// <summary> /// 디자이너 지원에 필요한 메서드입니다. /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(CustomAddIn_Startup ); this.Shutdown += new System.EventHandler(CustomAddIn_Shutdown); } #endregion #region 폴더 컨테이너 딕셔너리 구축하기 - BuildFolderContainerDictionary() /// <summary> /// 폴더 컨테이너 딕셔너리 구축하기 /// </summary> private void BuildFolderContainerDictionary() { foreach(Folder rootfolder in Application.ActiveExplorer().Session.Folders) { foreach(Folder folder in rootfolder.Folders) { AddFolderContainer(null, folder); } } } #endregion #region 폴터 컨테이너 추가하기 - AddFolderContainer(parentContainer, folder) /// <summary> /// 폴터 컨테이너 추가하기 /// </summary> /// <param name="parentContainer">부모 컨테이너</param> /// <param name="folder">폴더</param> private void AddFolderContainer(FolderContainer parentContainer, Folder folder) { string sourceKey = folder.FullFolderPath.Substring(2); string[] sourceKeyItemArray = sourceKey.Split('\\'); if(sourceKeyItemArray.Length < 2 || sourceKeyItemArray[1] != this.filterFolderName) { return; } FolderContainer container = new FolderContainer(); container.Parent = parentContainer; container.SourceKey = sourceKey; container.SourceFolder = folder; container.SourceFolders = folder.Folders; container.SourceFolders.FolderChange -= folders_FolderChange; container.SourceFolders.FolderChange += folders_FolderChange; if(parentContainer != null) { parentContainer.ChildList.Add(container); } this.folderContainerDictionary.Add(container.SourceKey, container); Debug.WriteLine(container.SourceKey); foreach(Folder childFolder in folder.Folders) { AddFolderContainer(container, childFolder); } } #endregion #region 폴더 컨테이너 딕셔너리 데이터 지우기 - ClearFolderContainerDictionaryData() /// <summary> /// 폴더 컨테이너 딕셔너리 데이터 지우기 /// </summary> private void ClearFolderContainerDictionaryData() { foreach(KeyValuePair<string, FolderContainer> keyValuePair in this.folderContainerDictionary) { string sourceKey = keyValuePair.Key; FolderContainer container = keyValuePair.Value; try { container.SourceFolders.FolderChange -= folders_FolderChange; } catch { } container.Parent = null; container.SourceKey = null; container.SourceFolder = null; container.SourceFolders = null; container.ChildList.Clear(); } this.folderContainerDictionary.Clear(); } #endregion } } |
※ 프로그램 실행 전, 계정 폴더 아래에 "Sales Force" 폴더를 사전에 생성한다.