How can I disable the Maximum Path Length Limitation and move a file into a folder with a long path name?
07:59 26 Jul 2025

I have vb.net winform application which creates a folder and moves some files into the folder. But when folder path goes long for example

E:\WF_2324\WF_2324_The Shadow Of The Sycamores\WF_2324_The Shadow Of The Sycamores_Part Four_1935  1943\WF_2324_The Shadow Of The Sycamores_Part Four_1935  1943_Chapter Thirty  Five_Null Name

and file name looks like e.g.

WF_2324_The Shadow Of The Sycamores_Part Four_1935  1943_Chapter Thirty  Five_Null Name.pdf

Even though I tried to disable the Maximum Path Length Limitation as shown in the MSFT documentation using group policy or by setting the registry key

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled   1

It didn't work, I still cannot move my files into the folder if the path is too long, instead I get an exception. How can I disable the maximum [ath length limitation in my application?

I am using .NET version 4.7.2 and 64 bit

My code:

 Private Sub BtnRenameFile_Click(sender As Object, e As EventArgs) Handles BtnRenameFile.Click
     Try
         If BgWorkerRenameFiles.IsBusy Then
             MessageBox.Show("Renaming is already in progress. Please wait.")
             Exit Sub
         End If

         ' Validation
         If Not Directory.Exists(TxtFolderPathM.Text.Trim()) Then
             MessageBox.Show("Folder does not exist.")
             Exit Sub
         End If

         If Not Integer.TryParse(TxtStartPage.Text.Trim(), Nothing) OrElse Not Integer.TryParse(TxtEndPage.Text.Trim(), Nothing) Then
             MessageBox.Show("Invalid page numbers.")
             Exit Sub
         End If

         Dim args As New RenameFileArgs With {
             .folderPath = TxtFolderPathM.Text.Trim(),
             .startPage = Integer.Parse(TxtStartPage.Text.Trim()),
             .endPage = Integer.Parse(TxtEndPage.Text.Trim())
             }
         ProgressBar1.Value = 0
         BgWorkerRenameFiles.RunWorkerAsync(args)
     Catch ex As Exception
         MessageBox.Show(ex.Message, "Error Found !!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
         Exit Sub
     End Try
 End Sub
 Private Sub BgWorkerRenameFiles_DoWork(sender As Object, e As DoWorkEventArgs) Handles BgWorkerRenameFiles.DoWork
     Try
         Dim args As RenameFileArgs = CType(e.Argument, RenameFileArgs)
         Dim folderPath = args.folderPath
         Dim startPage = args.startPage
         Dim endPage = args.endPage
         Dim pageCount As Integer = endPage - startPage + 1
         Dim cCode As String = GlobalSettings.ClientCode
         Dim folderName As String = New DirectoryInfo(folderPath).Name
         Dim tmpfilename As String = folderName.Substring(folderName.IndexOf("_"c) + 6).Trim()
         Dim finalFileName As String = $"{cCode}_{tmpfilename}"
         Dim pdfFiles As List(Of String) = Directory.GetFiles(folderPath, "*.pdf").OrderBy(Function(f) f).ToList()
         Dim tempFiles As New List(Of String)
         If pdfFiles.Count <> pageCount Then
             Throw New Exception("Mismatch in file count and page range.")
         End If
         ' Step 1: Temporary Rename
         For i As Integer = 0 To pdfFiles.Count - 1
             Dim oldPath As String = pdfFiles(i)
             Dim tempPath As String = Path.Combine(folderPath, $"_tempfile_{i}.pdf")
             File.Move(oldPath, tempPath)
             tempFiles.Add(tempPath)

             Dim percent = CInt(((i + 1) / pdfFiles.Count) * 50)
             BgWorkerRenameFiles.ReportProgress(percent)
         Next

         ' Step 2: Final Rename
         For i As Integer = 0 To tempFiles.Count - 1
             Dim pageNumber As Integer = startPage + i
             Dim newName As String = $"{finalFileName}_Page {pageNumber}.pdf"
             Dim newPath As String = Path.Combine(folderPath, newName)

             File.Move(tempFiles(i), newPath)

             Dim percent = 50 + CInt(((i + 1) / tempFiles.Count) * 50)
             BgWorkerRenameFiles.ReportProgress(percent)
         Next
         BgWorkerRenameFiles.ReportProgress(100)
     Catch ex As Exception
         MessageBox.Show("Error: " & ex.Message, "Renaming Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
         Exit Sub
     End Try
 End Sub
 Private Sub BgWorkerRenameFiles_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BgWorkerRenameFiles.ProgressChanged
     ProgressBar1.Value = e.ProgressPercentage
 End Sub
 Private Sub BgWorkerRenameFiles_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BgWorkerRenameFiles.RunWorkerCompleted
     MessageBox.Show("Files renamed successfully!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
     ProgressBar1.Value = 0
 End Sub

getting error when renaming file due to long path

windows vb.net winforms .net-4.7.2 max-path