Best way to resolve file path too long exception
Solution 1:
As the cause of the error is obvious, here's some information that should help you solve the problem:
See this MS article about Naming Files, Paths, and Namespaces
Here's a quote from the link:
Maximum Path Length Limitation In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string<NUL>" where "<NUL>" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)
And a few workarounds (taken from the comments):
There are ways to solve the various problems. The basic idea of the solutions listed below is always the same: Reduce the path-length in order to have path-length + name-length < MAX_PATH
. You may:
- Share a subfolder
- Use the commandline to assign a drive letter by means of SUBST
- Use AddConnection under VB to assign a drive letter to a path
Solution 2:
The solution that worked for me was to edit the registry key to enable long path behaviour, setting the value to 1. This is a new opt-in feature for Windows 10
HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)
I got this solution from a named section of the article that @james-hill posted.
https://docs.microsoft.com/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation