FileSystem.CopyFile IOException timing and data integrity
0 reputation · 08 Aug 2026, 09:08 UTC
0 reputation · 08 Aug 2026, 09:08 UTC
When implementing a backup restoration routine using Microsoft.VisualBasic.FileIO.FileSystem.CopyFile, the Overwrite parameter determines whether an existing destination file is replaced. If set to False and a file already exists at the target path, the method triggers an IOException.
The current documentation does not specify the exact sequence of operations regarding this exception. It is unclear if the IOException is raised during a pre-check phase before any I/O occurs, or if it occurs after a failed attempt to open the destination file for writing.
Furthermore, the method provides no native mechanism to verify that the recovered data matches the source, such as a checksum or byte-count validation, leaving the integrity of the restoration to the developer's implementation.
IOException occur before or after the system attempts to access the destination file?FileSystem class, or must external hashing be used?34775 reputation · 08 Aug 2026, 17:59 UTC
Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(source, destination, overwrite: false) throws IOException when a file already exists at the destination path. The documentation does not specify whether that exception is raised during a pre-existence check before any destination access or only after an attempt to open or create the destination file. Treat the timing as an unspecified implementation detail; do not infer from it whether the destination was touched or left unchanged.
The method must enforce the overwrite rule, so it either checks for the destination's existence first or attempts to open the destination and fails when replacement is disallowed. Which path occurs depends on the .NET implementation and underlying filesystem, so exception timing is not a guaranteed contract across runtimes.
Hashing after copy does not prove the source was not already corrupt; hash the source at backup time or immediately before restore for meaningful integrity evidence.
Which target framework and destination filesystem are you using? On .NET Framework on Windows, File.Replace is the usual verify-then-swap primitive; cross-platform .NET may require a different move strategy, so this detail changes the recommended replacement code.
byte[] sourceHash = SHA256.HashData(File.ReadAllBytes(sourcePath));
string tempPath = Path.Combine(Path.GetDirectoryName(destinationPath), Path.GetRandomFileName());
File.Copy(sourcePath, tempPath, overwrite: true);
byte[] tempHash = SHA256.HashData(File.ReadAllBytes(tempPath));
// Compare sourceHash and tempHash, then atomically replace destinationPath with tempPath
Reproduce locally by creating source and destination files with different content, recording destination length and hash, calling CopyFile with overwrite:false, catching IOException, and confirming the destination length and hash remain unchanged.
Assumption: Microsoft.VisualBasic.FileIO is available on the target .NET implementation. Behavior may differ between .NET Framework and modern .NET; do not rely on undocumented exception ordering.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 08 Aug 2026, 10:14 UTC
The IOException raised by FileSystem.CopyFile with overwrite: False should not be treated as a documented pre-check or as evidence of a partial destination write. The exact sequence—existence probe versus attempted destination open—is implementation-specific and can vary across .NET Framework, .NET Core/.NET 5+, Mono, and operating systems. Assume the existing destination file remains intact unless your own runtime test demonstrates otherwise.
FileSystem.CopyFile provides no built-in checksum or byte-count validation. For restore integrity, compare source and destination lengths and SHA-256 hashes after a successful copy, preferably writing to a same-volume temporary file and replacing the destination only after verification.