Using long paths (>260) with .net 4.6.2 in Windows service

2.4k Views Asked by At

I have some code that has begun to throw the exception: TooLongPathException. So I did some research and found out that .net 4.6.2 solves this problem. Great!

The code that fails is code that moves and copies folders to different folders. I would like to use .net 4.6.2 framework to enable this code to use longer paths so I do not need to code some workaround.

I have installed .net 4.6.2 framework on the machine. The machine runs Windows server 2008 R2 SP1. I have made the project target 4.6.2 framwork but still it throws this error.

I am not sure what I am missing here.

Has anyone used .net 4.6.2 for a similar thing that can point me as to what I need to do?

1

There are 1 best solutions below

1
grunge On

After a zillion of Tests I can confirm that this works with .Net 4.6.2 on a Windows10, but fails on a Server 2012r2.

When i want to delete a long folder that i created with Windows10 on a share, the Server is failing to delete it. My workaround is the old fashioned Robocopy.

public static void DirectoryDeleteRobocopy(string a_strPath)
    {
        _log.Debug("DirectoryDeleteRobocopy called: " + a_strPath);
        string strTmpDir = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        var emptyDirectory = new DirectoryInfo(strTmpDir + "\\TempEmptyDir_" + Guid.NewGuid());
        try
        {
            emptyDirectory.Create();
            using (var process = new Process())
            {
                process.StartInfo.FileName = "robocopy.exe";
                process.StartInfo.Arguments = "\"" + emptyDirectory.FullName + "\" \"" + a_strPath + "\" /e /purge";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.WaitForExit();
            }
            emptyDirectory.Delete();
            new DirectoryInfo(a_strPath).Attributes = FileAttributes.Normal;
            Directory.Delete(a_strPath);
        }
        catch (IOException) { }
    }

I think that Microsoft should make "Enable Win32 long paths" policy available to Server 2012 (and maybe even 2008) a soon as possible. Sorry, but right now this looks bad.