public byte[] DownloadFile(OneDriveContent oneDriveContent) { var uri = new Uri(Root + "/drive/items/" + oneDriveContent.Id + "/content"); var webRequest = WebRequest.Create(uri); webRequest.Headers.Add("Authorization", "Bearer " + OAuth2Token.access_token); var request = (HttpWebRequest)webRequest; request.Method = WebRequestMethods.Http.Get; try { var response = request.GetResponse(); using (var ms = new MemoryStream()) { response.GetResponseStream().CopyTo(ms); return(ms.ToArray()); } } catch (Exception excetion) { System.Windows.Forms.MessageBox.Show(excetion.Message); } return(null); }
private void buttonOK_Click(object sender, EventArgs e) { if (listViewFiles.SelectedItems.Count < 1) { return; } OneDriveContent sc = (OneDriveContent)listViewFiles.SelectedItems[0].Tag; try { if (listViewFiles.SelectedItems[0].Text == "..") { labelInfo.Text = "Loading files..."; Cursor = Cursors.WaitCursor; _roots.Pop(); LoadFiles(sc.ParentId); Cursor = Cursors.Default; } else if (sc.IsFolder) { labelInfo.Text = "Loading files..."; Cursor = Cursors.WaitCursor; _roots.Push(sc.ParentId); LoadFiles(sc.Id); Cursor = Cursors.Default; } else if (sc.IsFile) { labelInfo.Text = "Downloading..."; Refresh(); Cursor = Cursors.WaitCursor; var fileDown = _api.DownloadFile(sc); LoadedSubtitle = Encoding.UTF8.GetString(fileDown).Trim(); DialogResult = DialogResult.OK; } Cursor = Cursors.Default; labelInfo.Text = string.Empty; Refresh(); } catch (Exception exception) { labelInfo.Text = string.Empty; Cursor = Cursors.Default; MessageBox.Show(exception.Message); } }
public List <OneDriveContent> GetFiles(string path) { var uri = new Uri(Root + "/drive/root/children"); if (!string.IsNullOrEmpty(path)) { uri = new Uri(Root + "/drive/items/" + path + "/children"); } var json = GetResponse(uri); Hashtable o = (Hashtable)Nikse.Json.JSON.JsonDecode(json); List <OneDriveContent> list = new List <OneDriveContent>(); foreach (Hashtable ht in (o["value"] as ArrayList)) { OneDriveContent file = new OneDriveContent(); if (ht.ContainsKey("id")) { file.Id = ht["id"].ToString(); file.Name = ht["name"].ToString(); file.Size = Convert.ToInt64(ht["size"].ToString()); file.UpdatedTime = Convert.ToDateTime(ht["lastModifiedDateTime"].ToString()); if (ht["parentReference"] != null) { var parentElements = ht["parentReference"] as Hashtable; if (parentElements != null && parentElements["id"] != null) { file.ParentId = parentElements["id"].ToString(); } } if (ht["folder"] != null) { file.Type = "folder"; } else { file.Type = "file"; } list.Add(file); } } return(list); }
private void LoadFiles(string path) { listViewFiles.Items.Clear(); if (_roots.Count > 0) { ListViewItem item = new ListViewItem(".."); OneDriveContent sc = new OneDriveContent { ParentId = _roots.Peek() }; item.Tag = sc; item.ImageIndex = 1; listViewFiles.Items.Add(item); } foreach (OneDriveContent f in _api.GetFiles(path)) { if (f.IsFile || f.IsFolder) { ListViewItem item = new ListViewItem(f.Name) { Tag = f }; item.SubItems.Add(f.UpdatedTime.ToShortDateString() + " " + f.UpdatedTime.ToShortTimeString()); item.SubItems.Add(FormatBytesToDisplayFileSize(f.Size)); if (f.IsFile) { item.ImageIndex = 0; } else { item.ImageIndex = 1; } listViewFiles.Items.Add(item); } } if (listViewFiles.Items.Count > 0) { listViewFiles.Items[0].Selected = true; } }