File.Copy() throws with wrong error message when the source file is an Excel file and the destination is in a folder that does not exist
Description
Steps to reproduce:
- Create an Excel file called workbook.xlsx
- Open the file in Excel
- Run the following code while the file is opened
using System.IO;
namespace CopyFileBug
{
class Program
{
static void Main(string[] args)
{
File.Copy("workbook.xlsx", @"non_existing\test.xlsx");
}
}
}
Behavior: Throws a FileNotFoundException with the message "Could not find file 'workbook.xlsx'".
Expected behavior: Throws a DirectoryNotFoundException with the message "Could not find part of the path 'non_existing\test.xlsx'".
Configuration
Reproduced under Windows, on .NET 4.6.1 and .NET 5.0-windows.
Other information
In Win32FileSystem.cs:35 the check is too general. In the example at hand, the source file cannot be opened because it is used by another process, not because it doesn't exist.
A possible fix would be to check Marshal.GetLastWin32Error() when handle.IsInvalid == true to make sure that the source file is not valid because it doesn't exist and not for other reasons.
As a workaround you can do something like:
var parentDirectory = new DirectoryInfo(destination).Parent;
if (!parentDirectory?.Exists)
throw new DirectoryNotFoundException($"Part of the destination does not exist '{destination}'");
File.Copy(source, destination);
A possible fix would be to check Marshal.GetLastWin32Error() when handle.IsInvalid == true
Isn't that already done here? https://github.com/dotnet/corefx/blob/c55435befeb372707558a5c441b3c64bf243948f/src/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs#L46
I guess what I mean is -- what is the error code we would get ? ERROR_ACCESS_DENIED ?
Would an IOException saying access was denied to workbook.xlsx be equally reasonable?
- Open the file in Excel
@GeorgeMD Can you try reproducing the issue without opening the Excel file? Are you seeing the same error? Excel may be locking the file, preventing the copy. If you can still repro and the file cannot be copied into a non-existent folder, then we could consider the breaking change of changing the exception type and the message.
I can not confirm original scenario - I see always DirectoryNotFoundException with the simple repro C# app.
But in PowerShell console I get IOException and this suggests that the result depends on the application itself. Specifically, PowerShell catches the exception and does re-throw as IOException.
Perhaps this explains this issue too.
@danmoseley, when that line is run, fileName is already set to sourceFullPath, but the error should be about the destination. My suggestion was to add the check before setting fileName, to make sure the problem with the source file is that it doesn't exist.
@carlossanlop I managed to repro only using Excel and Word documents. And I tested 3 scenarios:
- File open in Excel, destination invalid => Exception stating that the source doesn't exist. (unexpected behavior, expected to see an error about the destination)
- File open in Excel, destination valid => No exception, the copy works. (expected behavior)
- File not open in Excel, destination invalid => Exception stating that the destination doesn't exist. (expected behavior)
- File not open in Excel, destination valid => No exception, the copy works. (expected behavior) By destination invalid I mean something like "C:\Folder that does not exist\file.xlsx", the destination is in a folder that doesn't exist.
But the source file is always checked first so the exception will be always first too. It looks like "Chicken-and-egg" problem.
My proposal is to keep checking the source first, but add an extra check next to source.IsInvalid that looks at Marshal.GetLastWin32Error() and if the error is ERROR_FILE_NOT_FOUND, only then it throws with "source not found", otherwise check the destination.
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.
This process is part of our issue cleanup automation.
This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.