PathTooLongException in C# code
Asked Answered
C

4

2

i have the following code:

 public static void Serialize()
    {

        List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
        List<string> dirFiles = new List<string>();
        foreach (string p in dirs)
        {
            string path = p;

            string lastAccessTime = File.GetLastAccessTime(path).ToString();


            bool DirFile = File.Exists(path);
            FileInfo fInf = new FileInfo(path);
            long lengthInk = fInf.Length / 1024;

            DateTime lastWriteTime = File.GetLastWriteTime(p);
            dirFiles.Add(p + "|" + lastAccessTime.ToString() + "|" + DirFile.ToString() + "|" + lastWriteTime.ToString() + "|" + lengthInk.ToString() + " kb");


        }

I keep hitting a PathTooLongException error with the following line:

string lastAccessTime = File.GetLastAccessTime(path).ToString();

The application drills into a drive and finds all files/folders w/in the drive. I cannot change this path but since it is above 260 characters...how to work around this?

Clinkerbuilt answered 6/7, 2009 at 15:17 Comment(3)
use windows api http://galratner.com/blogs/net/archive/2011/02/13/getting-around-pathtoolongexception-on-file-move-with-windows-native-api.aspxWyandotte
My own and other answers here suggest some wrapper libraries you can use for dealing with long paths.Murk
Possible duplicate of How to avoid System.IO.PathTooLongException?Vehemence
I
4

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.

Including answered 6/7, 2009 at 15:27 Comment(3)
I reached this response researching for a similar issue. But I find that SetCurrentDirectory also throws PathTooLongExceptipn. My question is at #4050699. Appreciate any comments you have.Imtiaz
@Hemal: SetCurrentDirectory() accepts relative paths. Have you tried splitting the operation into two calls? So, for example to navigate to C:\FirstPart\SecondPart\ThirdPart you would do: SetCurrentDirectory("C:\\FirstPart\\SecondPart"); SetCurrentDirectory(".\\ThirdPart");. By splitting the directory navigation into multiple steps you may be able to sidestep the path length limitations.Including
thanks for responding. Yes, I tried relative paths but that also gives the same exception when I reach the length limit.Imtiaz
H
2

Something like the .LastAccessTime property of Delimon.Win32.IO.FileInfo, might do the trick.

Delimon is a library on Microsoft TechNet for overcoming the long filenames problem, it's called Delimon.Win32.I​O Library (V4.0) and it has its own versions of key classes from System.IO

For example, you would replace:

System.IO.Directory.GetFiles 

with

Delimon.Win32.IO.Directory.GetFiles

which will let you handle long files and folders.

From the website:

Delimon.Win32.IO replaces basic file functions of System.IO and supports File & Folder names up to up to 32,767 Characters.

This Library is written on .NET Framework 4.0 and can be used either on x86 & x64 systems. The File & Folder limitations of the standard System.IO namespace can work with files that have 260 characters in a filename and 240 characters in a folder name (MAX_PATH is usually configured as 260 characters). Typically you run into the System.IO.PathTooLongException Error with the Standard .NET Library.

Helsinki answered 16/2, 2013 at 5:58 Comment(1)
Followed this advice yesterday but GetFileSystemInfos() threw not implemented exception. Also read that it's no longer maintained, and saw this suggested instead: github.com/alphaleonis/AlphaFSDiapason
A
0

.NET doesn't support Unicode file paths, so the only option I know of in this case is using P/Invoke (unless, of course, you can change the path) to call Win32 API functions that do support them. You can look here for instructions on how to use Unicode file path to break the 260 characters barrier.

Aloysia answered 6/7, 2009 at 15:19 Comment(0)
M
0

As Microsoft says here, there is a Windows limitation on 260 characters.

You can try to avoid this with a symbolic link (not sure...).

Matelda answered 6/7, 2009 at 15:21 Comment(1)
The link with the limitation actually contains the solution (see my answer to the question).Aloysia

© 2022 - 2024 — McMap. All rights reserved.