【发布时间】:2016-05-25 15:34:01
【问题描述】:
我正在开发一个 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 的文件夹。如果存在,则返回文件夹old 的Id,或者创建文件夹old,然后返回Id。
如您所见,我的方法看起来不是很好,但我不知道如何做得更好,也不让它运行(创建文件夹是这里的问题)。我可以上传一个空文件,从而自动创建文件夹,然后再次将其删除,但我不希望这样做。
我怎样才能更好、更专业地处理这个问题?提前谢谢你
【问题讨论】: