0

我有一些代码已经开始抛出异常:TooLongPathException。所以我做了一些研究,发现 .net 4.6.2 解决了这个问题。伟大的!

失败的代码是将文件夹移动和复制到不同文件夹的代码。我想使用 .net 4.6.2 框架使此代码能够使用更长的路径,因此我不需要编写一些解决方法。

我已经在机器上安装了 .net 4.6.2 框架。该机器运行 Windows server 2008 R2 SP1。我已将项目目标设为 4.6.2 框架,但仍会引发此错误。

我不确定我在这里缺少什么。

有没有人使用 .net 4.6.2 来做类似的事情,可以指出我需要做什么?

4

1 回答 1

0

经过无数次测试后,我可以确认这适用于 Windows10 上的 .Net 4.6.2,但在 Server 2012r2 上失败。

当我想删除在共享上使用 Windows10 创建的长文件夹时,服务器无法删除它。我的解决方法是老式的 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) { }
    }

我认为微软应该尽快为 Server 2012(甚至可能是 2008)提供“启用 Win32 长路径”策略。抱歉,但现在这看起来很糟糕。

于 2016-11-16T15:16:13.800 回答