0

I'm using the following code just to copy a file from a smaller filepath into a longer file path (> 260 characters).

string dbCDataPath = Path.Combine(DYRECTORY_WITH_GUARANTEE_ACCESS,Path.GetFileName(pathFileName));
string targetFile = Path.Combine(Path.GetDirectoryName(pathFileName), Path.GetFileName(pathFileName));
                    
File.Copy(dbCDataPath, targetFile, true);

I'm getting Could not find a part of the path error and I don't know why I have double checked both the source and the destination folders, both exists. Any help will be highly appreciated.

Andreea Elena
  • 29
  • 1
  • 7

1 Answers1

0

You need to call File.Copy like this:

File.Copy(targetFile, dbCDataPath, true);

The first parameter of this method is sourceFile you want to copy, the second parameter is destination path.

Limon M.
  • 21
  • 1
  • 2
  • But I want to copy the file from dbCDataPath in targetFile. – Andreea Elena Jun 10 '21 at 11:28
  • What is the value of pathFileName string? – Limon M. Jun 10 '21 at 11:44
  • C:\\Data\\Sandboxes\\AA-PP\\30_PRJ\\30_FND\\BBBB_S4\\40_SW\\30_Module_Test\\20_TRTPckTst\\10_Testcases\\New\\AA_PP_RRRRR_BBBB_S4_CfgPck_TestCases\\AA_PP_RRRRR_BBBB_S4_CfgPck_TestCases\\test_cases\\newFolderr\\bbbbbbbbbbbbbbbbbbbb\\ccccccccccccccc\\ddddddddddddddd\\tttttttttt.bak, taken from vs debug, it is a path > 260 characters – Andreea Elena Jun 10 '21 at 11:47
  • Did you enable long path in windows? If not, follow this: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd – Limon M. Jun 10 '21 at 12:00
  • You can check this answer: https://stackoverflow.com/questions/5188527/how-to-deal-with-files-with-a-name-longer-than-259-characters – Limon M. Jun 10 '21 at 12:03
  • Well I've tried to add in the app.config but the error is still there – Andreea Elena Jun 10 '21 at 12:24
  • I didn't perform any changes on windows, because I would like to achive the following use case , any user of the c# application shall be able to copy a file in another folder no matter how long the path is – Andreea Elena Jun 10 '21 at 12:25