C# 连接到Outlook日历时代码变慢
我写了这段代码,但有两个问题:C# 连接到Outlook日历时代码变慢,c#,calendar,outlook,C#,Calendar,Outlook,我写了这段代码,但有两个问题: 太慢了 它仅显示日历中的定期约会。我希望它不仅显示特定日期的定期约会,还显示该日期和指定日期的所有其他约会 非常感谢您的帮助 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Wi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void GetAllCalendarItems()
{
try
{
// outlook application object
Outlook.Application oApp = new Outlook.Application();
//folder in outlook
Outlook.Folders OutlookFolders = oApp.Session.Folders;
//Mapi folder object
Outlook.MAPIFolder Folder = null;
// outlook exploer object
Outlook.Explorer explorer = null;
//Map subfolder object
Outlook.MAPIFolder SubFolder = null;
// folder int control
int Folderctr;
// sub folder control
int subFolderctr;
// initialize base objects
//date range to get appointments
DateTime start = DateTime.Now;
DateTime end = start.AddDays(5);
// loop throught the PST files added on Outlook
for (Folderctr = 1; Folderctr <= OutlookFolders.Count; Folderctr++)
{
Folder = OutlookFolders[Folderctr];
Console.WriteLine("first name of folder " +Folder.Name);
explorer = Folder.GetExplorer();
//loop throught the folders in the PST files
for (subFolderctr = 1; subFolderctr <= explorer.CurrentFolder.Folders.Count; subFolderctr++)
{
SubFolder = explorer.CurrentFolder.Folders[subFolderctr];
if (SubFolder.Name == "SCE_Calendar")
{
GetAllCalendarItems(SubFolder);
Console.WriteLine("subfolder name of folder " + SubFolder.Name);
}
}
}
//close applciation
oApp.Quit();
// release com object
System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp);
oApp = null;
}
//Simple error handling.
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
public void GetAllCalendarItems(Outlook.MAPIFolder seCal)
{
Outlook.Items outlookCalendarItems = null;
//set recursion to true
outlookCalendarItems = seCal.Items;
outlookCalendarItems.IncludeRecurrences = true;
DateTime last;
DateTime first;
foreach (Outlook.AppointmentItem item in outlookCalendarItems)
{
Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
first = new DateTime(2012, 2, 1, item.Start.Hour, item.Start.Minute, 0);
last = new DateTime(2012, 2, 29);
Outlook.AppointmentItem recur = null;
for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
{
try
{
recur = rp.GetOccurrence(cur);
//Console.WriteLine(recur.Subject + " -> " + cur.ToLongDateString());
Console.WriteLine("Subject: " + recur.Body);
Console.WriteLine("Organizer: " + recur.Organizer);
Console.WriteLine("Start: " + recur.Start.ToString());
Console.WriteLine("End: " + recur.End.ToString());
Console.WriteLine("Location: " + recur.Location);
Console.WriteLine("Recurring: " + recur.IsRecurring);
Console.WriteLine("-----------------------------------------");
Console.WriteLine(" ");
//Show the item to pause.
}
catch
{ }
}
}
}
private void button1_Click(object sender, EventArgs e)
{
GetAllCalendarItems();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
运用系统反思;
使用Outlook=Microsoft.Office.Interop.Outlook;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
public void GetAllCalendarItems()
{
尝试
{
//outlook应用程序对象
Outlook.Application oApp=新建Outlook.Application();
//outlook中的文件夹
Outlook.Folders OutlookFolders=oApp.Session.Folders;
//Mapi文件夹对象
Outlook.MAPIFolder文件夹=null;
//outlook爆炸器对象
Explorer=null;
//映射子文件夹对象
Outlook.MAPIFolder子文件夹=null;
//文件夹int控件
int Folderctr;
//子文件夹控件
int子文件夹;
//初始化基本对象
//获取约会的日期范围
DateTime start=DateTime.Now;
DateTime end=start.AddDays(5);
//循环浏览Outlook上添加的PST文件
对于(Folderctr=1;Folderctr这是我用来获取Outlook约会的代码。我从一个工作程序中提取了它,因此我可能缺少一个助手函数,但这应该可以帮助您开始
private List<Outlook.AppointmentItem> GetOutlookAppointments(DateTime start, DateTime end) {
var outlook = new Microsoft.Office.Interop.Outlook.Application();
var app = outlook.Application;
var appSession = app.Session;
var calendar = appSession.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
string filter = String.Format("[Start] >= {0} And [End] < {1}", start.ToString("ddddd h:nn AMPM"), end.ToString("ddddd h:nn AMPM"));
var items = calendar.Items;
items.Restrict(filter);
items.IncludeRecurrences = true;
Outlook.AppointmentItem appointment;
var appointments = new List<Outlook.AppointmentItem>();
foreach (var item in items)
{
appointment = item as Outlook.AppointmentItem;
if (appointment != null && !appointment.AllDayEvent && (appointment.BusyStatus != Outlook.OlBusyStatus.olOutOfOffice))
{
if (appointment.IsRecurring)
{
AddReoccuringAppointments(appointments, start, end, appointment);
}
else if (appointment.Start >= start && appointment.End < end)
{
appointments.Add(appointment);
}
}
}
}
private static void AddReoccuringAppointments(List<Outlook.AppointmentItem> appointments, DateTime start, DateTime end, Outlook.AppointmentItem appointment)
{
Outlook.RecurrencePattern rp = appointment.GetRecurrencePattern();
if (rp.PatternStartDate >= end || rp.PatternEndDate <= start) { return; }
if (rp.PatternStartDate > start)
{
start = rp.PatternStartDate;
}
if (rp.PatternEndDate < end)
{
end = rp.PatternEndDate;
}
var exceptions = GetExceptions(rp, start, end);
Outlook.AppointmentItem recur = null;
Outlook.Exception exception = null;
for (DateTime cur = new DateTime(start.Year, start.Month, start.Day, appointment.Start.Hour, appointment.Start.Minute, 0); cur <= end; cur = cur.AddDays(1))
{
if ((((int)rp.DayOfWeekMask) & (int)Math.Pow(2,(int)cur.DayOfWeek)) == 0) { continue; }
exception = exceptions.FirstOrDefault(e => e.OriginalDate.Date == cur.Date);
if (exception == null)
{
recur = rp.GetOccurrence(cur);
}
else if (!exception.Deleted)
{
recur = exception.AppointmentItem;
}
if (recur != null)
{
appointments.Add(recur);
}
}
}
private static List<Outlook.Exception> GetExceptions(Outlook.RecurrencePattern rp, DateTime start, DateTime end)
{
List<Outlook.Exception> exceptions = new List<Outlook.Exception>();
foreach (Outlook.Exception e in rp.Exceptions)
{
if (e.OriginalDate >= start && e.OriginalDate < end)
{
exceptions.Add(e);
}
}
return exceptions;
}
私人列表GetOutlookAppointment(日期时间开始,日期时间结束){
var outlook=new Microsoft.Office.Interop.outlook.Application();
var-app=outlook.Application;
var appSession=app.Session;
var calendar=appSession.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
string filter=string.Format(“[Start]>={0}和[End]<{1}”、Start.ToString(“ddddd h:nn AMPM”)、End.ToString(“ddddd h:nn AMPM”);
var items=日历项;
限制(过滤);
items.includeCurrences=true;
Outlook.AppointmentItem约会;
var约会=新列表();
foreach(项目中的var项目)
{
约会=项目作为Outlook.AppointmentItem;
if(appointment!=null&&!appointment.AllDayEvent&&(appointment.BusyStatus!=Outlook.OlBusyStatus.olOutOfOffice))
{
如果(约会正在进行)
{
添加重复约会(约会、开始、结束、约会);
}
else if(appointment.Start>=Start&&appointment.End=结束| | rp.PatternEndDate开始)
{
开始=rp.patterns开始日期;
}
如果(rp.patterneddate=开始和&e.OriginalDate
发布您所有的代码并寻求帮助改进这不是一个真正的建设性问题。我建议您改进您的问题,以显示导致您出现问题的代码部分,并详细描述您遇到的问题。