Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从谷歌日历api编写文本文件_C#_Google Calendar Api - Fatal编程技术网

C#从谷歌日历api编写文本文件

C#从谷歌日历api编写文本文件,c#,google-calendar-api,C#,Google Calendar Api,我需要一些帮助。可能很容易,但我不明白。 在我们的消防站中有一个屏幕,显示警报(地址、关键字等)。当没有警报时,我想显示即将到来的事件,如生日等。运行屏幕的软件需要输入“.txt”-文件。 所以我想做的是:我创建一个谷歌日历,把所有的事件都放进去。每天晚上,任务调度器运行一小段软件,从谷歌下载事件,将所有内容写入一个文件,警报软件读取该文件 到目前为止,我已经做到了: 我创建了一个谷歌日历api 我从谷歌下载了一个代码示例,并让它与两个日历一起工作 就这样。现在我被困了一整天,我想我是一个比

我需要一些帮助。可能很容易,但我不明白。 在我们的消防站中有一个屏幕,显示警报(地址、关键字等)。当没有警报时,我想显示即将到来的事件,如生日等。运行屏幕的软件需要输入“.txt”-文件。 所以我想做的是:我创建一个谷歌日历,把所有的事件都放进去。每天晚上,任务调度器运行一小段软件,从谷歌下载事件,将所有内容写入一个文件,警报软件读取该文件

到目前为止,我已经做到了:

  • 我创建了一个谷歌日历api
  • 我从谷歌下载了一个代码示例,并让它与两个日历一起工作
就这样。现在我被困了一整天,我想我是一个比程序员更好的消防员。所以我需要帮助。请给我一些提示怎么做

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
对象(可能名为
ListOfLines
),最初为空,然后继续为您希望文件包含的每一行添加一个字符串。然后,当列表中的所有行都已包含(按适当顺序)时,只需使用
file.writeallines(文件名,listofstring)
,将使用给定的文件名创建一个文件,其行将是列表中的字符串

据我所见,您只需将
Console.Writeline(somestring)
替换为
ListOfLines.Add(somestring)
。 另外,请注意,您不需要显式地在字符串中添加换行符,
File.WriteLines
会为您这样做