4

My open source software synchronizes a remote folder to the local desktop. The remote folder can be on Alfresco, where path length has no limit (/root/very/very/very/long/name.txt).

When developing the application I used System.IO.File.OpenWrite and System.IO.Stream.Write, but it appears that they don't support paths over a few hundred characters, so users report errors like PathTooLongException when working with long paths.

UNC paths must absolutely be avoided, because they lead to incompatibility with some applications.

What library/code should be used in this case?

It must be open source C#.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • I just noticed you did the "post your own answer" feature. I am willing to bet you where pounding your head for a while figuring this out and now you want to share on how you solved it so others do not have to suffer. [I have been there](http://stackoverflow.com/q/11346554/80274). – Scott Chamberlain Feb 19 '13 at 08:03

5 Answers5

3

AlphaFS 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().

(source)

Community
  • 1
  • 1
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
2

You can try the Base Class Libraries Long Path implementation. But be aware that not everything will work if you start to mix up with default .NET I/O methods.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • This is the recommended solution. +1 Have used extensively before. I recall a bug in there at the time of the original release, but it's open source and I'm sure it's been since officially fixed. – Mahmoud Al-Qudsi Feb 19 '13 at 08:04
  • +1 Great! The license is Microsoft Public License (Ms-PL) which is open source, even though it is unfortunately not compatible with the GNU-GPL: http://www.gnu.org/licenses/license-list.html#ms-pl – Nicolas Raoul Feb 19 '13 at 08:05
1

".NET 2.0 Workaround for PathTooLongException" (article+code).

Licensed under The Code Project Open License (CPOL), which is not open source apparently.

Only 2 commits do not make for a very active project, though...

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • 1
    Ummm, its not really a "project" its just telling you to use a PInvoke to call `CreateFileW` and use `\\?\.` at the start of your path name. – Scott Chamberlain Feb 19 '13 at 07:47
  • I take it back, I looked at the source, he does put some work in to it. I would not be concerned about the "not too active" part. You are just wrapping the Windows API with a few helper functions, once you got rid of your bugs, there is not much more to do – Scott Chamberlain Feb 19 '13 at 07:49
1

Native File System Access

Since the revision 185 (February 19th, 2013), the library is covered by Microsoft Public License (MS-PL), as specified in the headers of the source code files.

Looks promising, but author advises against using it in an enterprise environment, since the code is not solid enough.

This library has some unit tests, but lacks documentation.

Community
  • 1
  • 1
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • I can't recommend this library. Basic functionality, like `File.Open` doesn't work (disposes the handle of the stream it returns and ignores the `access`/`share` parameters) and file enumeration silently ignores errors. – CodesInChaos Jun 27 '15 at 11:57
1

You could try splitting the path up by using relative paths as outlined in this answer using:

Directory.SetCurrentDirectory()

PathTooLongException in C# code

Community
  • 1
  • 1
markoo
  • 708
  • 1
  • 6
  • 22