I have been trying to figure out a solution for working with really long file paths that are beyond the scope of the regular Windows API Like System.IO.PathToLongException Problem. I read the blog described in the answer MS Blog On Long Paths, but when using that path format I'm still getting the exception for paths that are to long. Am I doing something wrong?
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace CreateTestDirForLongPaths
{
class Program
{
public static string MainFolder = @"L:\users\"insert username here"\desktop\LPTD";
public static bool Finished = false;
public static int FolderCounter = 0;
public static string PLD = @"L:\users\"insert username here"\desktop\LPTD\0";
public static string CName = Environment.MachineName.ToString();
public static string ComputerNamePre = @"\\" + CName + "\\";
static void Main(string[] args)
{
DirectoryInfo Source = new DirectoryInfo(MainFolder);
CreateTree(Source);
PLD = PLD.Substring(3, PLD.Length -4);
string LD = ComputerNamePre + @"L$" + "\\" + PLD + "\\" + "Folder Beyond reach";
try
{
Directory.CreateDirectory(LD);
}
catch(Exception e)
{
Console.WriteLine("End Error:" + "\n\n" + e.Message + "\n\n" + LD);
}
Console.WriteLine("\n\nFinished.");
Console.ReadKey(true);
}
static void CreateTree(DirectoryInfo directory)
{
try
{
MakeSubDirs(directory);
}
catch (IOException)
{
Finished = true;
}
}
static void MakeSubDirs(DirectoryInfo directory)
{
string CurrentDir = null;
try
{
string NewDir = directory.FullName + "\\" + FolderCounter.ToString();
CurrentDir = directory.FullName;
FolderCounter = ++FolderCounter;
PLD = PLD + "\\" + FolderCounter.ToString();
Directory.CreateDirectory(NewDir);
DirectoryInfo NextDir = new DirectoryInfo(NewDir);
CreateTree(NextDir);
}
catch (IOException)
{
Finished = true;
try
{
Process.Start(CurrentDir);
}
catch(Exception e)
{
Console.WriteLine("Start Error" + "\n\n" + e.Message + CurrentDir);
}
}
}
}
}
Notes: The Application above is a console app for creating a folder that's just out of reach for the regular Windows API without using the Unicode version of the API for testing another app that modifies folders in different ways on a fileshare. I get the PathToLongException on Line 26 of the code when it tries to create the folder using the UNC Path format. Any help would be greatly appreciated.
Summary Of Problem: I need a way to work with paths exceeding the normal 248 character limit for folders and 260 for files in the regular Windows API.