How to Avoid System.Io.Pathtoolongexception

How to avoid System.IO.PathTooLongException?

As described in Jeremy Kuhne's blog, .NET Framework 4.6.2 removes the MAX_PATH limitation where possible, without breaking backwards compatibility.

PathTooLongException in C# code

The GetLastAccessTime() call, with a full path can exceed the internal limit (which is OS-version specific, but typically 260 characters) on the maximum length for a fully qualified file path.

One way to avoid this, is to use Directory.SetCurrentDirectory() to change the current system directory and then call GetLastAccessTime() with only a relative path. Just make sure you change your current directory back to what you started from to avoid unexpected issues.

PathTooLongException C# 4.5

Yes, using the standard APIs will give you this kind of limitations (255 chars IIRC).

From .NET you can use the AlphaFS project which lets you use very long paths (using the "\\?\" style) and mimics the System.IO namespace.

You will probably be able to use this library just as if you were using System.IO, for example : AlphaFS.Win32.Filesystem.File.Copy() instead System.IO.File.Copy()

If you don't want or cannot use AlphaFS you'll have to pinvoke the Win32 API

Best way to resolve file path too long exception

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

VisualStudio PathTooLongException

I somehow got rid of this error message. I don't understand how, but let me share steps I did.

  1. I started removing projects from solution one by one to figure out what project causes the issue.
  2. Once I found the bad one, I reverted all the changes on GIT.

  3. I repeated this approach for files and folders within this project. As I deleted some files and restarted the VS, the problem disappeared.

  4. Again reverted all changes on GIT.

  5. Voila, the error is history in both 2017 and 2019. Nothing changed in SLN or CSPROJ files.



Related Topics



Leave a reply



Submit