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

OneDrive-API create folder if not exist

我正在开发 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;
}
}

代码的作用是什么?我输入了 Class,例如ClassA,然后它会在 Special Folder 中查找,其中只有我的应用程序可以访问此 ClassA 并搜索名为 old 的文件夹。如果它存在,它是 returns 文件夹 oldId 或者它创建文件夹 old 然后 returns 它 Id.

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

我怎样才能更好、更专业地处理这个问题?提前谢谢你

I put in a Class, e.g. ClassA, then it looks in the Special Folder, where only my App has access for this ClassA and searches for a folder called old. If it exists, it's returns the Id of folder old or it creates folder old and then returns it Id.

我无法理解你的 ClassA 这里,你的意思是你想在你的应用程序的 Class 中完成这项工作?

总之,我这里写了一个例子,可以获取到我的app在OneDrive的文件夹,看看里面是否有名为"old"的子文件夹,如果有,return 这个文件夹的 ID,如果没有,创建一个和 return 这个新文件夹的 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的点击事件中,如果你想在Class中访问它,你可以尝试把这段代码放在你的[=32]中=] 的构造函数。

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