C# 从 google 日历 api 写入文本文件
C# write textfile from google calendar api
我需要一些帮助。可能这很容易,但我不明白。
在我们的消防站中有一个显示警报的屏幕(地址、关键字……)。当没有警报时,我想显示即将发生的事件,如生日等。软件 运行 屏幕需要“.txt”文件进行输入。
所以我想做的是:我创建一个 google 日历并将所有事件放入其中。每天晚上,任务调度程序都会运行一小段软件并从 google 下载事件,将所有内容写入一个文件,然后警报软件会读取该文件。
到现在我已经走了这么远:
- 我创建了一个 google 日历 api。
- 我从 google 下载了一个代码示例,并使用了两个日历。
就是这样。现在我整天都被困住了,我想我是一个比程序员更好的消防员。所以我需要帮助。请给我一些提示如何去做。
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CalendarQuickstart
{
class Program
{
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request. CALENDAR
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// END REQUEST
// Define parameters of request. BIRTHDAY
EventsResource.ListRequest request1 = service.Events.List("#contacts@group.v.calendar.google.com");
request1.TimeMin = DateTime.Now;
request1.ShowDeleted = false;
request1.SingleEvents = true;
request1.MaxResults = 10;
request1.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// END REQUEST
// List events. CALENDAR
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
} // END LISTE
// List events. BIRTHDAY
Events events1 = request1.Execute();
Console.WriteLine("Upcoming events:");
if (events1.Items != null && events1.Items.Count > 0)
{
foreach (var eventItem in events1.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
} // END LISTE
else
{
Console.WriteLine("No upcoming events found.");
}
Console.Read();
}
}
}
我建议您创建一个 List<String>
对象(可能命名为 ListOfLines
,最初是空的,然后继续为您希望文件包含的每一行添加一个字符串。然后,当列表中已经包含所有行(以适当的顺序)时,只需使用 File.WriteAllLines(filename, ListOfStrings)
,将使用给定的文件名创建一个文件,其行将是列表中的字符串。
据我所知,您只需将当前代码中的 Console.Writeline(somestring)
替换为 ListOfLines.Add(somestring)
。
另外,请注意,您不需要在字符串中显式添加换行符,File.WriteLines
会为您完成。
我需要一些帮助。可能这很容易,但我不明白。 在我们的消防站中有一个显示警报的屏幕(地址、关键字……)。当没有警报时,我想显示即将发生的事件,如生日等。软件 运行 屏幕需要“.txt”文件进行输入。 所以我想做的是:我创建一个 google 日历并将所有事件放入其中。每天晚上,任务调度程序都会运行一小段软件并从 google 下载事件,将所有内容写入一个文件,然后警报软件会读取该文件。
到现在我已经走了这么远:
- 我创建了一个 google 日历 api。
- 我从 google 下载了一个代码示例,并使用了两个日历。
就是这样。现在我整天都被困住了,我想我是一个比程序员更好的消防员。所以我需要帮助。请给我一些提示如何去做。
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CalendarQuickstart
{
class Program
{
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request. CALENDAR
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// END REQUEST
// Define parameters of request. BIRTHDAY
EventsResource.ListRequest request1 = service.Events.List("#contacts@group.v.calendar.google.com");
request1.TimeMin = DateTime.Now;
request1.ShowDeleted = false;
request1.SingleEvents = true;
request1.MaxResults = 10;
request1.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// END REQUEST
// List events. CALENDAR
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
} // END LISTE
// List events. BIRTHDAY
Events events1 = request1.Execute();
Console.WriteLine("Upcoming events:");
if (events1.Items != null && events1.Items.Count > 0)
{
foreach (var eventItem in events1.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
} // END LISTE
else
{
Console.WriteLine("No upcoming events found.");
}
Console.Read();
}
}
}
我建议您创建一个 List<String>
对象(可能命名为 ListOfLines
,最初是空的,然后继续为您希望文件包含的每一行添加一个字符串。然后,当列表中已经包含所有行(以适当的顺序)时,只需使用 File.WriteAllLines(filename, ListOfStrings)
,将使用给定的文件名创建一个文件,其行将是列表中的字符串。
据我所知,您只需将当前代码中的 Console.Writeline(somestring)
替换为 ListOfLines.Add(somestring)
。
另外,请注意,您不需要在字符串中显式添加换行符,File.WriteLines
会为您完成。