Click here to Skip to main content
15,878,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on core 3.1 web api

I get exception error as below :

Exception The target file is a directory, not a file. on file.copy

this lines generated exception
System.IO.File.Copy(OutputfilePath, Path.Combine(exportDirectory, fileName), true);

Path.Combine(exportDirectory, fileName) is \\\ImportExport\testImporter\5\orignal-86f3c73b-a859-482a-bb62-d68808564cf3.xlsx

OutputfilePath is \\\ImportExport\testImporter\5\OutputFile.xlsx

two files can access and open it when I open on file explorer but issue

as above

so How to solve this issue please ?

What I have tried:[^]
Updated 30-Jun-22 22:21pm
Richard MacCutchan 25-May-21 3:48am    
Are you sure that Path.Combine(exportDirectory, fileName) contains that exact string?
ahmed_sa 25-May-21 4:14am    
thank you for reply
what you mean by exact string
Richard Deeming 25-May-21 4:25am    
As RMC said, debug your code and verify that Path.Combine(exportDirectory, fileName) returns the expected destination file path, and nothing else.

Verify that you don't already have a directory in the export directory with the same name as the destination file.

Verify that the account your application is running as has access to read and write files in the specified paths. You'll need to check both the NTFS settings on the folder, and the permissions on the share.
ahmed_sa 25-May-21 4:31am    
can you more clear please
destination and source is files and can access it
so why error give me it is directory
the destination and source is file and not directory
it is ended by xlsx excel file
so why error give me it is directory
Richard Deeming 25-May-21 8:09am    
Just because it ends with .xlsx doesn't mean it's definitely a file. Directories can contain . characters too.

1 solution

I know this is an old topic, but maybe it will save some time to someone.
I was just having the same problem. Turns out I was calling

where path had the file name in it. But this actually creates a folder with a name the filename.
So the exception message was true: it was indeed a directory, not a file.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900