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?

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
yeahumok
  • 2,940
  • 19
  • 52
  • 63
  • use windows api [http://galratner.com/blogs/net/archive/2011/02/13/getting-around-pathtoolongexception-on-file-move-with-windows-native-api.aspx](http://galratner.com/blogs/net/archive/2011/02/13/getting-around-pathtoolongexception-on-file-move-with-windows-native-api.aspx) – julian Feb 06 '12 at 14:53
  • My own and other answers [here](http://stackoverflow.com/a/29605805/589059) suggest some wrapper libraries you can use for dealing with long paths. – rkagerer Apr 13 '15 at 13:32
  • Possible duplicate of [How to avoid System.IO.PathTooLongException?](http://stackoverflow.com/questions/530109/how-to-avoid-system-io-pathtoolongexception) – Deantwo Mar 14 '17 at 12:42

4 Answers4

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.

LBushkin
  • 129,300
  • 32
  • 216
  • 265
  • I reached this response researching for a similar issue. But I find that SetCurrentDirectory also throws PathTooLongExceptipn. My question is at http://stackoverflow.com/questions/4050199/directory-setcurrentdirectory-throws-pathtoolongexception. Appreciate any comments you have. – Miserable Variable Oct 29 '10 at 11:11
  • @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. – LBushkin Oct 29 '10 at 14:46
  • thanks for responding. Yes, I tried relative paths but that also gives the same exception when I reach the length limit. – Miserable Variable Oct 30 '10 at 07:34
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.

TripleAntigen
  • 2,221
  • 1
  • 31
  • 44
  • Followed this advice yesterday but GetFileSystemInfos() threw not implemented exception. Also read that it's no longer maintained, and saw this suggested instead: https://github.com/alphaleonis/AlphaFS – Bunjy Nov 17 '16 at 15:53
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.

On Freund
  • 4,376
  • 2
  • 23
  • 30
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...).

FerranB
  • 35,683
  • 18
  • 66
  • 85