In NTFS, I can prefix a path with the \\?\
character sequence to denote that it is a path that exceeds the 260-character limit; as such, the file system will interpret the path correctly and avoid raising PathTooLongException
.
(see http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath for more information)
Is there a .NET API that will prefix my path strings with this sequence, or am I stuck writing my own?
In essence, I am looking for a method that is equivalent to the following.
static string ToExtendedPath(string path)
{
if(Path.IsPathRooted(path))
{
return @"\\?\" + path;
}
return Path.Combine(@"\\?\", path);
}
No, there is no .NET API that translates a given "normal" path into the extended syntax. You have to roll your own (which is trivial, by the way).
Please note: As Cody Gray and Hans Passant mentioned, the .NET framework does not support long (extended) paths. If you want to work with them, you need to use the API directly. And not all API functions support long paths either. Generally, the low-level functions do. Consult the MSDN documentation.
What I have done is write wrapper functions for the relevant API functions (e.g. CreateFile) and call those wrappers instead of the .NET file system functions.