google calendar C# window application sample(根据google代码改造)
1.饮用 google calendar dll
2.create a new form named Form1;
3.add three textbox in this form, one for username, one for password, one for message(多行);
4.add a button in this form;
5.copy code .
#define USE_TRACING
#define DEBUG
using System;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Net;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
namespace GoogleCalendarTest_HYC
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox UserName;
private System.Windows.Forms.TextBox PassWord;
private System.Windows.Forms.TextBox CalendarContent;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.CalendarContent = new System.Windows.Forms.TextBox();
this.UserName = new System.Windows.Forms.TextBox();
this.PassWord = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// CalendarContent
//
this.CalendarContent.Location = new System.Drawing.Point(24, 72);
this.CalendarContent.Multiline = true;
this.CalendarContent.Name = "CalendarContent";
this.CalendarContent.Size = new System.Drawing.Size(720, 424);
this.CalendarContent.TabIndex = 0;
this.CalendarContent.Text = "";
//
// UserName
//
this.UserName.Location = new System.Drawing.Point(178, 24);
this.UserName.Name = "UserName";
this.UserName.TabIndex = 1;
this.UserName.Text = "@gmail.com";
//
// PassWord
//
this.PassWord.Location = new System.Drawing.Point(378, 24);
this.PassWord.Name = "PassWord";
this.PassWord.PasswordChar = '*';
this.PassWord.TabIndex = 2;
this.PassWord.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(96, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 3;
this.label1.Text = "Username:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(296, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Password:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(496, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 23);
this.button1.TabIndex = 5;
this.button1.Text = "Show Your Calendar";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(776, 509);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.PassWord);
this.Controls.Add(this.UserName);
this.Controls.Add(this.CalendarContent);
this.Name = "Form1";
this.Text = "GoogleCalendar";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
public const string ApplicationName = "GoogleCalendar/1.0.0";
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//
// TODO: Add code to start application here
//
if(this.UserName.Text=="")
{
CalendarContent.Text ="Please input your username.";
return;
}
if(this.PassWord.Text=="")
{
CalendarContent.Text ="Please input your password.";
return;
}
string calendarURI = "http://www.google.com/calendar/feeds/"+this.UserName.Text +"/private/full";
string userName = this.UserName.Text ;
string passWord = this.PassWord.Text ;
FeedQuery query = new FeedQuery();
CalendarService service = new CalendarService(ApplicationName);
if (userName != null)
{
service.setUserCredentials(userName, passWord);
}
GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory) service.RequestFactory;
// temporary measure
factory.MethodOverride = true;
query.Uri = new Uri(calendarURI);
EventFeed calFeed = service.Query(query);
if (calFeed.Location != null)
{
CalendarContent.Text =CalendarContent.Text+"Feed Location: " + calFeed.Location.ValueString;
}
CalendarContent.Text =CalendarContent.Text+"User Agent: " + factory.UserAgent;
foreach (EventEntry feedEntry in calFeed.Entries )
{
CalendarContent.Text =CalendarContent.Text+"";
CalendarContent.Text =CalendarContent.Text+"Appointment: " + feedEntry.Title.Text;
foreach (When when in feedEntry.Times)
{
CalendarContent.Text =CalendarContent.Text+"When: " + when.StartTime + " " + when.EndTime + " " + when.ValueString;
}
foreach (Where where in feedEntry.Locations)
{
CalendarContent.Text =CalendarContent.Text+"Where: " + where.Rel + " " + where.Label + " " + where.ValueString;
}
foreach (Who who in feedEntry.Participants)
{
CalendarContent.Text =CalendarContent.Text+"Who: " + who.Rel + " " + who.ValueString;
if (who.Attendee_Status != null)
CalendarContent.Text =CalendarContent.Text+"Who Status: " + who.Attendee_Status.Value;
if (who.Attendee_Type != null)
CalendarContent.Text =CalendarContent.Text+"Who Type: " + who.Attendee_Type.Value;
}
if (feedEntry.EventVisibility != null)
CalendarContent.Text =CalendarContent.Text+"Visibility: " + feedEntry.EventVisibility.Value;
if (feedEntry.EventTransparency != null)
CalendarContent.Text =CalendarContent.Text+"Transparency: " + feedEntry.EventTransparency.Value;
if (feedEntry.Status != null)
CalendarContent.Text =CalendarContent.Text+"Status: " + feedEntry.Status.Value;
if (feedEntry.Recurrence != null)
CalendarContent.Text =CalendarContent.Text+"Recurrence: " + feedEntry.Recurrence.Value;
if (feedEntry.Comments != null)
CalendarContent.Text =CalendarContent.Text+"Comments: " + feedEntry.Comments.FeedLink.Href;
if (feedEntry.OriginalEvent != null)
{
CalendarContent.Text =CalendarContent.Text+"OriginalEvent - ID: " + feedEntry.OriginalEvent.IdOriginal;
CalendarContent.Text =CalendarContent.Text+"OriginalEvent - Href: " + feedEntry.OriginalEvent.Href;
CalendarContent.Text =CalendarContent.Text+"OriginalEvent - startTime: " + feedEntry.OriginalEvent.OriginalStartTime.StartTime;
}
if (feedEntry.Reminder != null)
{
if (feedEntry.Reminder.Days > 0)
CalendarContent.Text =CalendarContent.Text+"Reminder - Days: " + feedEntry.Reminder.Days.ToString();
if (feedEntry.Reminder.Hours > 0)
CalendarContent.Text =CalendarContent.Text+"Reminder - Hours: " + feedEntry.Reminder.Hours.ToString();
if (feedEntry.Reminder.Minutes > 0)
CalendarContent.Text =CalendarContent.Text+"Reminder - Minutes: " + feedEntry.Reminder.Minutes.ToString();
if (feedEntry.Reminder.AbsoluteTime != new DateTime(1,1,1))
CalendarContent.Text =CalendarContent.Text+"Reminder - AbsoluteTime: " + feedEntry.Reminder.AbsoluteTime;
}
}
XmlTextWriter writer = new XmlTextWriter("calendar.xml", null);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(false);
if (calFeed != null)
{
calFeed.SaveToXml(writer);
}
writer.Flush();
writer.Close();
}
catch
{
MessageBox.Show ("Error!");
}
}
}
}