{"record":{"id":"6cfb3eb3e7913ae4","repo":"chocolatey/choco","slug":"an-exception-occurred-while-copying-files-to-0","errorCode":null,"errorMessage":"An exception occurred while copying files to '{0}'","messagePattern":"An exception occurred while copying files to '(.+?)'","errorType":"exception","errorClass":"AggregateException","httpStatus":null,"severity":"error","filePath":"src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs","lineNumber":790,"sourceCode":"                var destinationFile = file.Replace(sourceDirectoryPath, destinationDirectoryPath);\r\n                EnsureDirectoryExists(GetDirectoryName(destinationFile), ignoreError: true);\r\n                //this.Log().Debug(ChocolateyLoggers.Verbose, \"Copying '{0}' {1} to '{2}'\".FormatWith(file, Environment.NewLine, destinationFile));\r\n\r\n                try\r\n                {\r\n                    CopyFile(file, destinationFile, overwriteExisting, isSilent);\r\n                }\r\n                catch (Exception ex)\r\n                {\r\n                    exceptions.Add(ex);\r\n                }\r\n            }\r\n\r\n            Thread.Sleep(1500); // sleep for enough time to allow the folder to finish copying\r\n\r\n            if (exceptions.Count > 1)\r\n            {\r\n                throw new AggregateException(\"An exception occurred while copying files to '{0}'\".FormatWith(destinationDirectoryPath), exceptions);\r\n            }\r\n            else if (exceptions.Count == 1)\r\n            {\r\n                throw exceptions[0];\r\n            }\r\n        }\r\n\r\n        public void EnsureDirectoryExists(string directoryPath)\r\n        {\r\n            EnsureDirectoryExists(directoryPath, false);\r\n        }\r\n\r\n        public bool IsLockedDirectory(string directoryPath)\r\n        {\r\n            try\r\n            {\r\n                var permissions = Directory.GetAccessControl(directoryPath);\r\n\r","sourceCodeStart":772,"sourceCodeEnd":808,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs#L772-L808","documentation":"Thrown by DotNetFileSystem's directory-copy routine as an AggregateException when more than one individual file copy failed during a recursive copy. The method catches each per-file exception into a list, waits to let I/O settle, then wraps all collected exceptions into a single AggregateException whose message names the destination directory.","triggerScenarios":"Copying a directory tree where two or more files throw (lock, permission, missing source, path-too-long). Each failure is appended to the exceptions list; when exceptions.Count > 1 the AggregateException is raised with the inner list preserved.","commonSituations":"Antivirus or another process holding files open mid-copy, insufficient NTFS permissions on a subset of files, very long paths exceeding MAX_PATH on Windows, or a source tree partially locked by a running Chocolatey operation.","solutions":["Inspect AggregateException.InnerExceptions to identify the actual failing files and root causes (lock, access denied, path length).","Close other processes/AV scanning the directory, grant the running account read access, and retry.","Enable long-path support or shorten the destination path if path-too-long is among the inner exceptions."],"exampleFix":"// before\ntry { fs.CopyDirectory(src, dest, true, false); }\ncatch (Exception ex) { log(ex.Message); } // loses inner failures\n\n// after\ntry { fs.CopyDirectory(src, dest, true, false); }\ncatch (AggregateException ex) {\n  foreach (var inner in ex.InnerExceptions) log(inner.GetType().Name + \": \" + inner.Message);\n  throw;\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check: ensure source files are readable and not locked before bulk copy.\nvar unreadable = srcFiles.Where(f => {\n    try { using (File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)) {} return false; }\n    catch { return true; }\n}).ToList();\nif (unreadable.Count > 0)\n    throw new IOException(unreadable.Count + \" source file(s) not readable/locked: \" + string.Join(\", \", unreadable));","typeGuard":null,"tryCatchPattern":"try\n{\n    fs.CopyDirectory(src, dest, overwriteExisting, isSilent);\n}\ncatch (AggregateException ex)\n{\n    foreach (var inner in ex.InnerExceptions)\n        logger.Error(\"Copy failed: \" + inner.GetType().Name + \": \" + inner.Message);\n    throw;\n}","preventionTips":["Close/AV-exclude the target directory before copying to reduce lock contention.","Surface AggregateException.InnerExceptions rather than only the top Message to diagnose multi-file failures."],"tags":["filesystem","copy","aggregate-exception","io"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}