Download All Files and Folders using CSharp
Hi All,
We had a Requirement to download all the Folders and Files of an User from Box using C#.
PFB code to recursively download all the Folders and Files
Hope this is helpful!
using Box.V2;
using Box.V2.Config;
using Box.V2.JWTAuth;
using Box.V2.Models;
using BoxApi.V2;
using BoxApi.V2.Authentication.OAuth2;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CBN.SP.Box
{
class Program
{
static void Main(string[] args)
{
ExecuteMainAsync().Wait();
}
///
/// Function to call other functions to create the Folder structure and Files
///
///
private static async Task ExecuteMainAsync()
{
try
{
IBoxConfig config = null;
using (FileStream fs = new FileStream("D:\\HubEnterpriseConfig.json", FileMode.Open))
{
config = BoxConfig.CreateFromJsonFile(fs);
}
var session = new BoxJWTAuth(config);
//var client = session.AdminClient(session.AdminToken());
var client = session.AdminClient(session.AdminToken());
var boxUsers = await client.UsersManager.GetEnterpriseUsersAsync();
List allBoxUsersList = boxUsers.Entries;
var login = "***email address removed for privacy***";
var specificBoxUser = allBoxUsersList.Find(u => u.Login == login);
Console.WriteLine("A Specific Box User: {0}", specificBoxUser.Name);
//Console.WriteLine("Box User Name: {0} and Box User Id: {1} and Login: {2}", boxUser.Name, boxUser.Id, boxUser.Login);
var userToken = session.UserToken(specificBoxUser.Id);
var userClient = session.UserClient(userToken, specificBoxUser.Id);
string drive = "D:\\";
var userRootFolderPath = Path.Combine(drive, specificBoxUser.Name);
// CreateFolder(userRootFolderPath);
string directory = Directory.GetCurrentDirectory().ToString();
var FolderID = "8***phone number removed for privacy***";
await getFolders(FolderID, userClient, userRootFolderPath);
// await downloadFiles("0", userClient, userRootFolderPath);
Console.ReadLine();
}
catch (Exception)
{
throw;
}
}
///
/// Get All the Folders within a User's Account in Box.com
///
///
///
///
///
private static async Task getFolders(string FolderID, BoxClient client, string cdir)
{
try
{
var folder = await client.FoldersManager.GetInformationAsync(FolderID);
var folderName = folder.Name;
var items = await client.FoldersManager.GetFolderItemsAsync(FolderID, 1000, autoPaginate: true);
var localFolderPath = Path.Combine(cdir, folderName);
//Calling a Function to create the Folder
//CreateFolder(localFolderPath);
Console.WriteLine(folder.PathCollection.ToString());
Console.WriteLine("Created Folder : " + localFolderPath.ToString());
var folders = items.Entries.Where(i => i.Type == "folder");
foreach (var Sfolder in folders)
{
var subFolder = await client.FoldersManager.GetFolderItemsAsync(Sfolder.Id, 1000, autoPaginate: true);
var clocalFolderPath = localFolderPath.ToString();
//Recursive call to create the Folder Structure
await getFolders(Sfolder.Id, client, clocalFolderPath);
}
}
catch (Exception)
{
throw;
}
}
///
/// To download all the Files within a User's Account in Box.com
///
///
///
///
///
private static async Task downloadFiles(string FolderID, BoxClient client, string cdir)
{
try
{
var folder = await client.FoldersManager.GetInformationAsync(FolderID);
var folderName = folder.Name;
var items = await client.FoldersManager.GetFolderItemsAsync(FolderID, 1000, autoPaginate: true);
var localFolderPath = Path.Combine(cdir, folderName);
var files = items.Entries.Where(i => i.Type == "file");
foreach (var file in files)
{
using (FileStream fileStream = new FileStream(localFolderPath + "\\" + file.Name, FileMode.Create, System.IO.FileAccess.Write))
{
using (Stream stream = await client.FilesManager.DownloadStreamAsync(file.Id))
{
//Downloading the current File
await stream.CopyToAsync(fileStream);
}
}
Console.WriteLine("Downloaded File : " + file.Name);
}
var folders = items.Entries.Where(i => i.Type == "folder");
foreach (var Sfolder in folders)
{
//Recursive call to download all the Files
await downloadFiles(Sfolder.Id, client, localFolderPath);
}
}
catch (Exception)
{
throw;
}
}
///
/// To create a Folder in the respective Directory
///
///
private static void CreateFolder(string localFolderPath)
{
try
{
if (!Directory.Exists(localFolderPath))
{
Directory.CreateDirectory(localFolderPath);
}
/*
else
{
foreach (var file in Directory.EnumerateFiles(localFolderPath))
{
File.Delete(Path.Combine(localFolderPath, file));
}
Directory.Delete(localFolderPath);
Directory.CreateDirectory(localFolderPath);
}
*/
}
catch (Exception)
{
throw;
}
}
}
}
-
Hi !
Just wanted to thank you so much for sharing your code with the community! This is an extremely helpful post for our community members and will continue to help others who are looking for a way to download a user's folders and files.
Thank you again for your contribution and for your time in the community!
Emma
Box Community Management Intern
投稿コメントは受け付けていません。
コメント
1件のコメント