public void Start(object o) { string logpath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Covererror.log"); if (File.Exists(logpath)) { File.Delete(logpath); } StreamWriter sr = new StreamWriter(logpath); try { ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); if (string.IsNullOrEmpty(Properties.Settings.Default.Domain)) { service.Credentials = new WebCredentials(Properties.Settings.Default.EXIMPUser, Properties.Settings.Default.EXIMPPassword); } else { service.Credentials = new WebCredentials(Properties.Settings.Default.EXIMPUser, Properties.Settings.Default.EXIMPPassword, Properties.Settings.Default.Domain); } if (string.IsNullOrEmpty(Properties.Settings.Default.ExchangeUri)) { service.AutodiscoverUrl(Properties.Settings.Default.EXIMPUser, RedirectionUrlValidationCallback); } else { service.Url = new Uri(Properties.Settings.Default.ExchangeUri + "/ews/exchange.asmx"); } List <Appointment> Appointments = new List <Appointment>(); int count = 0; for (var i = 0; i <= Properties.Settings.Default.AdditionalCoverDays; i++) { XmlDocument doc = new XmlDocument(); //try //{ DateTime dt = DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Saturday ? DateTime.Now.AddDays(i + 2) : DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Sunday ? DateTime.Now.AddDays(i + 1) : DateTime.Now.AddDays(i); doc.Load(Path.Combine(Properties.Settings.Default.CoverUNC, "CV" + dt.ToString("ddMMyy") + ".htm")); XmlDocument doc1 = new XmlDocument(); doc1.Load(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "staffmapping.xml")); count += doc.SelectNodes("/html/body/table/tr[@bgcolor='#f1f1f1']").Count; } if (Initialized != null) { Initialized(count); } for (var i = 0; i <= Properties.Settings.Default.AdditionalCoverDays; i++) { XmlDocument doc = new XmlDocument(); //try //{ DateTime dt = DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Saturday ? DateTime.Now.AddDays(i + 2) : DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Sunday ? DateTime.Now.AddDays(i + 1) : DateTime.Now.AddDays(i); doc.Load(Path.Combine(Properties.Settings.Default.CoverUNC, "CV" + dt.ToString("ddMMyy") + ".htm")); XmlDocument doc1 = new XmlDocument(); doc1.Load(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "staffmapping.xml")); List <string> clearedaccounts = new List <string>(); foreach (XmlNode n in doc.SelectNodes("/html/body/table/tr[@bgcolor='#f1f1f1']")) { try { CoverRow r = CoverRow.Parse(n); if (r.Cover != "No Cover Reqd") { string email = ""; foreach (XmlNode c in doc1.SelectNodes("/staffmappings/staff[@last=\"" + r.Cover.Split(new char[] { ',' })[0] + "\"]")) { if (r.Cover.ToLower().EndsWith(c.Attributes["first"].Value.ToLower().ToCharArray()[0].ToString())) { email = c.Attributes["email"].Value; break; } } if (!string.IsNullOrEmpty(email)) { service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email); if (!clearedaccounts.Contains(email)) { SearchFilter.SearchFilterCollection searchFilter = new SearchFilter.SearchFilterCollection(); searchFilter.Add(new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, dt.Date)); searchFilter.Add(new SearchFilter.ContainsSubstring(AppointmentSchema.Subject, "Cover:")); ItemView view = new ItemView(999); view.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.AppointmentType); FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Calendar, searchFilter, view); foreach (Item item in findResults.Items) { ((Appointment)item).Delete(DeleteMode.HardDelete); } clearedaccounts.Add(email); } Appointment a = CreateApp(r, dt, service); if (a != null) { a.Save(); } } } this.Progress++; if (Updated != null) { Updated(); } } catch (Exception ex) { sr.WriteLine(ex.Message); sr.WriteLine(ex.Source); sr.WriteLine(ex); } } } } catch (Exception e) { sr.WriteLine(e.Message); sr.WriteLine(e.Source); sr.WriteLine(e); } finally { sr.Close(); } if (Done != null) { Done(); } }