OneDrive-API创建文件夹(如果不存在)

时间:2016-05-23 17:52:20

标签: uwp onedrive

我正在使用Windows 10 UWP应用程序,该应用程序使用c#中的OneDrive API

我试图获取特定子文件夹的Id或创建它(如果它不存在)。到目前为止它看起来像这样:

public async Task<string> getOldFolder<IDBEntry>(IDBEntry objectType)
{
try
{
    // authenticate
    if (!this.oneDriveClient.IsAuthenticated)
    {
        await this.oneDriveClient.AuthenticateAsync();
    }

    var children = await oneDriveClient.Drive.Special.AppRoot.Children.Request().Select("name,id").GetAsync(); // get name and id only (dont need the rest)
    string retID = string.Empty;

    string ID = string.Empty;
    if (children != null)
    {
        foreach (var item in children)
        {
            var s = item.Name;
            if (s == objectType.GetType().Name) // class name, e.g. "classA"
            {
                ID = item.Id;
            }
        }
    }

    // cancel, if nothing found
    if (ID == string.Empty)
    {
        return null;
    }

    children = await oneDriveClient.Drive.Items[ID].Children.Request().GetAsync();
    if (children != null)
    {
        foreach (var item in children)
        {
            if (item.Name == "old")
            {
                retID = item.Id;
            }
        }
    }

    if (string.IsNullOrEmpty(retID))
    {
        // create folder "old"
        // this is the part I'm stuck with
        string itemPath = objectType.GetType().Name + "/" + "old";
        Item item = await oneDriveClient.Drive.Special.AppRoot.ItemWithPath(itemPath).Content.Request().PutAsync
        if (item != null && (!string.IsNullOrEmpty(item.Id)))
        {
            retID = item.Id;
        }
    }

    return retID;
}
catch (Exception)
{
    return null;
}
}

代码是做什么的?我输入了一个类,例如ClassA,然后查看Special Folder,只有我的应用可以访问此ClassA,并搜索名为old的文件夹。如果存在,则会返回文件夹Id的{​​{1}},或者创建文件夹old,然后将其返回old

正如你所看到的,我的方法不是很好看,但我不知道,如何做得更好,也不让它运行(创建文件夹是这里的问题)。我可以上传一个空文件,从而自动创建文件夹,然后再将其删除,但我不希望这样做。

我如何更好,更专业地处理这个问题?提前Tahnk你

1 个答案:

答案 0 :(得分:1)

  

我输入了一个类,例如ClassA,然后它在特殊文件夹中查找,只有我的App可以访问此ClassA并搜索名为old的文件夹。如果存在,则返回旧文件夹的Id或创建旧文件夹,然后将其返回Id。

我无法理解您的ClassA,您的意思是您希望在应用的Class内完成这项工作?

无论如何,我在这里写了一个示例,它可以在OneDrive中获取我的应用程序文件夹,并查找是否有一个名为&#34; old&#34;的子文件夹。如果是,则返回此文件夹的ID,如果没有,则创建一个并返回该新文件夹的ID。

private string ID;

private async void Button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var appfolderitems = await GetAppRoot();
        Dictionary<string, string> itemList = new Dictionary<string, string>();
        foreach (var item in appfolderitems)
        {
            var id = item.Id;
            var name = item.Name;
            itemList.Add(id, name);
        }
        if (itemList.ContainsValue("old"))
        {
            foreach (var item in itemList)
            {
                if (item.Value == "old")
                    ID = item.Key;
            }
        }
        else
        {
            ID = await CreateFolder();
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}

private IOneDriveClient driveClient;

private async Task<IChildrenCollectionPage> GetAppRoot()
{
    driveClient =
        await OneDriveClientExtensions.GetAuthenticatedUniversalClient(
            new string[] { "onedrive.readwrite", "offline_access" }
            );

    if (!driveClient.IsAuthenticated) return null;

    var itemRequest = await driveClient.Drive.Special.AppRoot.Children.Request().GetAsync();
    return itemRequest;
}

private async Task<string> CreateFolder()
{
    if (!driveClient.IsAuthenticated) return null;
    var folderToCreate = new Item { Name = "old", Folder = new Folder() };
    var oldfolder = await driveClient.Drive.Special.AppRoot.Children.Request().AddAsync(folderToCreate);
    return oldfolder.Id;
}

正如您在此处所看到的,我将用于获取ID的代码放在Button click事件中,如果要在Class中访问它,您可以尝试将此代码放在类的构造函数中。

顺便说一下,我没有在这里使用搜索和过滤器Api来查找文件夹,我的目的是区分名为&#34; old&#34;和文件名为&#34; old&#34;,例如&#34; old.txt&#34;,默认情况下,如果您搜索&#34; old&#34;,&#34; old.txt&#34; ;也将被添加到结果中。目前我还没有找到使用Q过滤器搜索特定类型的方法,您可以尝试一下。

相关问题