■ MAPIFolderEvents_12_Event 인터페이스의 BeforeFolderMove/BeforeItemMove 이벤트를 사용해 폴더/메일의 삭제/이동을 방지하는 방법을 보여준다.
▶ 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
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 소스 폴더 폴더 이동 전 처리하기 - sourceFolder_BeforeFolderMove(targetFolder, cancel) /// <summary> /// 소스 폴더 폴더 이동 전 처리하기 /// </summary> /// <param name="targetFolder">타겟 폴더</param> /// <param name="cancel">취소 여부</param> private void sourceFolder_BeforeFolderMove(MAPIFolder targetFolder, ref bool cancel) { cancel = true; } #endregion #region 소스 폴더 항목 이동 전 처리하기 - sourceFolder_BeforeItemMove(sourceItem, targetFolder, cancel) /// <summary> /// 소스 폴더 항목 이동 전 처리하기 /// </summary> /// <param name="sourceItem">소스 항목</param> /// <param name="targetFolder">타겟 폴더</param> /// <param name="cancel">취서 여부</param> private void sourceFolder_BeforeItemMove(object sourceItem, MAPIFolder targetFolder, ref bool cancel) { cancel = true; } #endregion #region 소스 폴더 컬렉션 폴더 변경시 처리하기 - sourceFolders_FolderChange(folder) /// <summary> /// 소스 폴더 컬렉션 폴더 변경시 처리하기 /// </summary> /// <param name="folder">폴더</param> private void sourceFolders_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.SourceFolder.BeforeFolderMove -= sourceFolder_BeforeFolderMove; container.SourceFolder.BeforeItemMove -= sourceFolder_BeforeItemMove; container.SourceFolders.FolderChange -= sourceFolders_FolderChange; container.SourceFolder.BeforeFolderMove += sourceFolder_BeforeFolderMove; container.SourceFolder.BeforeItemMove += sourceFolder_BeforeItemMove; container.SourceFolders.FolderChange += sourceFolders_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.SourceFolder.BeforeFolderMove -= sourceFolder_BeforeFolderMove; container.SourceFolder.BeforeItemMove -= sourceFolder_BeforeItemMove; container.SourceFolders.FolderChange -= sourceFolders_FolderChange; } catch { } container.Parent = null; container.SourceKey = null; container.SourceFolder = null; container.SourceFolders = null; container.ChildList.Clear(); } this.folderContainerDictionary.Clear(); } #endregion } } |