经过无数次测试后,我可以确认这适用于 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 长路径”策略。抱歉,但现在这看起来很糟糕。