here's extension method checks if folder created and:
- if created returns folder id.
- if not created, creates , returns folder id.
you can use id upload , download folder.
public async static task<string> createdirectoryasync(this liveconnectclient client, string foldername, string parentfolder) { string folderid = null; // retrieves directories. var queryfolder = parentfolder + "/files?filter=folders,albums"; var opresult = await client.getasync(queryfolder); dynamic result = opresult.result; foreach (dynamic folder in result.data) { // checks if current folder has passed name. if (folder.name.tolowerinvariant() == foldername.tolowerinvariant()) { folderid = folder.id; break; } } if (folderid == null) { // directory hasn't been found, creates using postasync method. var folderdata = new dictionary<string, object>(); folderdata.add("name", foldername); opresult = await client.postasync(parentfolder, folderdata); result = opresult.result; // retrieves id of created folder. folderid = result.id; } return folderid; }
you use as:
string skydrivefolder = await createdirectoryasync(liveconnectclient, "<yourfoldernamehere>", "me/skydrive");
now skydrivefolder has folder id can use when uploading , downloading. here's sample upload:
liveoperationresult result = await liveconnectclient.uploadasync(skydrivefolder, filename, filestream, overwriteoption.overwrite);
addition complete answer ynotdraw
using provided, here's how download text file specifying file name. below not include if file not found , other potential exceptions, here works when stars align properly:
public async static task<string> downloadfileasync(this liveconnectclient client, string directory, string filename) { string skydrivefolder = await onedrivehelper.createorgetdirectoryasync(client, directory, "me/skydrive"); var result = await client.downloadasync(skydrivefolder); var operation = await client.getasync(skydrivefolder + "/files"); var items = operation.result["data"] list<object>; string id = string.empty; // search file - add handling here if file not found foreach (object item in items) { idictionary<string, object> file = item idictionary<string, object>; if (file["name"].tostring() == filename) { id = file["id"].tostring(); break; } } var downloadresult= await client.downloadasync(string.format("{0}/content", id)); var reader = new streamreader(downloadresult.stream); string text = await reader.readtoendasync(); return text; }
and in usage:
var result = await downloadfile(_client, "mydir", "backup.txt");