Skip to content

Instantly share code, notes, and snippets.

@cartermp
Last active May 11, 2016 12:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cartermp/e2950be56ef3341ade56 to your computer and use it in GitHub Desktop.
Save cartermp/e2950be56ef3341ade56 to your computer and use it in GitHub Desktop.

Maxpath and .NET

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

project.json

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":{}
	}
}

Program.cs

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);
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment