Changes to .NET Core now allow for longer paths for directories. Here's a sample showcasing that.
Note: this sample is running on .NET Core. The easiest way to get going with that is to get the bits, create these files in a directory, and open that directory with Visual Studio Code.
To build and run:
$ dnu restore
$ dnu build
$ dnx run
This contains the NuGet dependencies needed to run the sample.
{
"dependencies": {
"System.Runtime":"4.0.20-rc1-*",
"System.Console": "4.0.0-beta-23516",
"System.IO":"4.0.0-rc1-*",
"System.IO.FileSystem":"4.0.0-rc1-*"
},
"frameworks": {
"dnxcore50":{}
}
}
Here's the sample!
using System;
using System.IO;
namespace MaxPath
{
public class Program
{
public void Main(string[] args)
{
// Paths can now be created/manipulated over the legacy 260 character limit.
//
// On Windows, the max length is now ~32k!
//
// .NET used to only be able to create directories 248 characters deep.
// This made 254 characters impossible even if the total length was under 260.
var directory = Directory.CreateDirectory(
Path.Combine(Directory.GetCurrentDirectory(),
new string('d', 254)));
// The max segment length on Windows is 255, so let's do that!
//
// Note: UDF still requires 254 (optical media uses 254)
string path = Path.Combine(directory.FullName, new string('f', 254));
File.WriteAllText(path, "Really long paths!");
Console.WriteLine(File.ReadAllText(path));
Console.WriteLine($"Path length is: {path.Length}");
directory.Delete(recursive: true);
}
}
}