그래서 재귀 적으로 디렉토리를 만들고 폴더의 파일을 전체적으로 다운로드하는 코드가 있습니다. 루프는 else
의 첫 번째 파일을 발견 한 후 그러나 그것은 foreach
에 예외를 발생, 그것을 다운로드하고 다음 파일로 이동sharpbox를 사용하여 파일 다운로드
public class Program
{
static void UploadDownloadProgress(Object sender, FileDataTransferEventArgs e)
{
// print a dot
Console.WriteLine("Downloading");
Console.WriteLine(".");
// it's ok to go forward
e.Cancel = false;
}
public static void DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, string targetDir, string sourceDir)
{
foreach (ICloudFileSystemEntry fsentry in remoteDir)
{
var filepath = Path.Combine(targetDir, fsentry.Name);
if (fsentry is ICloudDirectoryEntry)
{
Console.WriteLine("Created: {0}", filepath);
Directory.CreateDirectory(filepath);
DownloadFolder(dropBoxStorage, fsentry as ICloudDirectoryEntry, filepath, sourceDir);
}
else
{
dropBoxStorage.DownloadFile(remoteDir, fsentry.Name, targetDir, UploadDownloadProgress);
}
}
}
static void Main(string[] args)
{
CloudStorage dropBoxStorage = new CloudStorage();
var dropBixConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox);
ICloudStorageAccessToken accessToken = null;
using (FileStream fs = File.Open(@"C:\Users\Michael\token.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
accessToken = dropBoxStorage.DeserializeSecurityToken(fs);
}
var storageToken = dropBoxStorage.Open(dropBixConfig, accessToken);
//do stuff
//var root = dropBoxStorage.GetRoot();
var publicFolder = dropBoxStorage.GetFolder("/Public");
foreach (var folder in publicFolder)
{
Boolean bIsDirectory = folder is ICloudDirectoryEntry;
Console.WriteLine("{0}: {1}", bIsDirectory ? "DIR" : "FIL", folder.Name);
}
string remoteDirName = @"/Public/IQSWS";
string targetDir = @"C:\Users\Michael\";
var remoteDir = dropBoxStorage.GetFolder(remoteDirName);
DownloadFolder(dropBoxStorage, remoteDir, targetDir, remoteDirName);
dropBoxStorage.Close();
}
public delegate void FileOperationProgressChanged(object sender, FileDataTransferEventArgs e);
}
: 여기서
코드이다System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Collection was modified; enumeration operation may not execute.
Source=mscorlib
StackTrace:
at System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()
at StartServer.Program.DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, String targetDir, String sourceDir) in c:\Users\Michael\Documents\Visual Studio 2012\Projects\IQS Source\StartServer\StartServer\Program.cs:line 29
at StartServer.Program.DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, String targetDir, String sourceDir) in c:\Users\Michael\Documents\Visual Studio 2012\Projects\IQS Source\StartServer\StartServer\Program.cs:line 39
at StartServer.Program.DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, String targetDir, String sourceDir) in c:\Users\Michael\Documents\Visual Studio 2012\Projects\IQS Source\StartServer\StartServer\Program.cs:line 39
at StartServer.Program.DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, String targetDir, String sourceDir) in c:\Users\Michael\Documents\Visual Studio 2012\Projects\IQS Source\StartServer\StartServer\Program.cs:line 39
at StartServer.Program.Main(String[] args) in c:\Users\Michael\Documents\Visual Studio 2012\Projects\IQS Source\StartServer\StartServer\Program.cs:line 80
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
왜 이런 일이 발생합니까? 파일을 다운로드 할 때 배열이 편집되는 이유는 무엇입니까?