0

I'm having some issues on moving a file, that has been written using another software's API, to a path that is very long.

The destination path is created using

Dim path As String = "\\?\C:\Basedir\Long-path-base"

For Each folderName As String In pathList
    currentPath = IO.Path.Combine(currentPath.Trim(), folderName.Trim())
    CreateDirectoryW(currentPath.Trim(), IntPtr.Zero)
Next

As you can see, I'm using CreateDirectoryW due to the >256 character path. pathList is a simple List(Of String) with foldernames corresponding to the final path for the current file.

with pathList = {'folder1','folder2',folder3','....'} the path will then be

C:\Basedir\Long-path-base\folder1\folder2\folder3....

The above works very well. The problem comes when I want to copy the file that has been written to My.Computer.FileSystem.SpecialDirectories.Temp to currentPath. The file is written nicely to the temp-directory, but not moved to the path above.

I do:

Dim sourceFile As String = 
My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.Temp, filename & ".ddb")
        
Dim destinationFile As String = My.Computer.FileSystem.CombinePath(path, filename & ".ddb")
        
Dim success As Boolean = MoveFileW(sourceFile, destinationFile.Remove(0, 4))

One thing I don't understand is if I should have "\\?\" added to the sourcepath, or removed from the destinationpart (as is done above). I have tried both, but without success. The reason why I'm not writing the file to the destinationfolder directly is that the API does not support path's longer than 256 characters.

I'm using .NET 4.7.2

Any help would be great!

Tobbe
  • 11
  • 3
  • 2
    https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry#enable-long-paths-in-windows-10-version-1607-and-later – Hans Passant Mar 02 '23 at 16:29
  • Yes, I have been through that page many times, which finally helped me getting creating paths with more characters than 256 to work. The problem I have now is coping files to the created path – Tobbe Mar 02 '23 at 21:09
  • Shouldn't this `currentPath = IO.Path.Combine(currentPath.Trim(), folderName.Trim())` be this `currentPath = IO.Path.Combine(path.Trim(), folderName.Trim())`? – F0r3v3r-A-N00b Mar 02 '23 at 21:54

0 Answers0