以递归方式下载所有内容的文件夹,Sharpbox

时间:2013-10-04 13:55:50

标签: c# dropbox-api sharpbox

所以我设置了一个小应用程序,我想用它从我的公共Dropbox文件夹下载一个文件夹,并将其中的所有内容下载到虚拟机。

如果我尝试:

var publicFolder = dropBoxStorage.GetFolder("/Public");
string targetFile = @"C:\Users\Michael\";
dropBoxStorage.DownloadFile(publicFolder,@"WS",targetFile);

WS如果文件夹包含我要下载的所有内容。

然而,当我运行代码时,我得到: enter image description here

2 个答案:

答案 0 :(得分:1)

SharpBox不支持下载文件夹。 我花时间写了一个函数,它应该递归下载文件夹。 (虽然没有测试过。)

string remoteDirName = @"/Public/WS";
string targetDir = @"C:\Users\Michael\";
var remoteDir = dropBoxStorage.GetFolder(remoteDirName);

public static DownloadFolder(CloudStorage dropBoxStorage,ICloudDirectoryEntry remoteDir, string targetDir)
{

    foreach (ICloudFileSystemEntry fsentry in remoteDir)
    {
        if (fsentry is ICloudDirectoryEntry)
        {
            DownloadFolder(dropBoxStorage, fsentry, Path.Combine(targetDir, fsentry.Name));
        }
        else
        {
            dropBoxStorage.DownloadFile(remoteDir,fsentry.Name,Path.Combine(targetDir, fsentry.Name));
        }
    }
}

答案 1 :(得分:0)

下面的代码也适用..

var PublicFolder = dropBoxStorage.GetFolder("/Public");
 if (PublicFolder != null && PublicFolder.ToList().Count > 0)
                {
 DownloadFolder(dropBoxStorage, PublicFolder as ICloudDirectoryEntry, targetPath);
}



public static void DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, string targetDir)
    {
        foreach (var fof in remoteDir.ToList())
        {

            if (fof is ICloudDirectoryEntry)
            {
                DirectoryInfo newDir = new DirectoryInfo(Path.Combine(targetDir, fof.Name));
                if (!newDir.Exists)
                {
                    Directory.CreateDirectory(Path.Combine(targetDir, fof.Name));
                }

                DownloadFolder(dropBoxStorage, fof as ICloudDirectoryEntry, Path.Combine(targetDir, fof.Name));
            }
            else
            {
                dropBoxStorage.DownloadFile(remoteDir, fof.Name, Path.Combine(targetDir));
            }

        }
    }