Click here to Skip to main content
15,032,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on asp.net 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
C#
System.IO.File.Copy(OutputfilePath, Path.Combine(exportDirectory, fileName), true);

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

OutputfilePath is \\192.168.2.7\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:

https://docs.microsoft.com/en-us/answers/storage/attachments/99491-image.png[^]
Posted
Updated 25-May-21 1:03am
v5
Comments
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.
ahmed_sa 25-May-21 5:21am
   
can we more clear please

both destination and source is files not directory

GetFilesDownload is

\\\\192.168.2.7\\ImportExport\\testImporter
string Month = DateTime.Now.Month.ToString();
string exportDirectory = GetFilesDownload + "\\" + Month;


var OutputfilePath = System.IO.Path.Combine( GetFilesDownload, "DeliveryGeneration_Output.xlsx");

System.IO.File.Copy(OutputfilePath, Path.Combine(exportDirectory, fileName), true);
Richard MacCutchan 25-May-21 7:51am
   
Please show the exact strings as your code runs. All you need to do is to add some print/debug statements so you can verify that what you expect in those fields is exactly what the code creates.
ahmed_sa 25-May-21 8:40am
   
System.IO.File.Copy(OutputfilePath, Path.Combine(exportDirectory, fileName), true);
System.Diagnostics.Debug.WriteLine(myValue2)
\\192.168.2.7\ImportExport\testImporter
System.Diagnostics.Debug.WriteLine(exportDirectory)
\\192.168.2.7\ImportExport\testImporter\5
System.Diagnostics.Debug.WriteLine(OutputfilePath)
\\192.168.2.7\ImportExport\testImporter\DeliveryGeneration_Output.xlsx
System.Diagnostics.Debug.WriteLine(Path.Combine(exportDirectory, fileName))
\\192.168.2.7\ImportExport\testImporter\5\orignal-e1f75892-7c35-4533-b2ff-ddc8e9893733.xlsx
System.Diagnostics.Debug.WriteLine(ex)
System.IO.IOException: The target file '\\192.168.2.7\ImportExport\testImporter\5\orignal-e1f75892-7c35-4533-b2ff-ddc8e9893733.xlsx' is a directory, not a file.
at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
at Framework.WebApi.Controllers.DeliverySystem.DeliverySysController.Upload()
Richard MacCutchan 25-May-21 9:04am
   
Well whatever configuration you have the system is definitely seeing your target as a directory. It could be something to do with the fact that the target is on an IP address, rather than a local disk, but it is impossible to be certain.
Richard MacCutchan 25-May-21 9:05am
   
BTW, do you see that little button above our messages titled Reply?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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