我有以下代码:
static long getFolderSize(string path)
{
string[] a = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
long b = 0;
foreach (string name in a)
{
FileInfo fi = new FileInfo(name);
b += fi.Length;
}
return b;
}在运行此代码的环境中,有超过260个字符限制的路径。因此,行
FileInfo fi = new FileInfor(name);抛出一个System.IO.PathTooLongException。
我已经阅读了很多关于这个问题的文章,根据https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/的说法,这个问题应该在.NET 4.6.2中解决。因此,我在.NET 4.7中编译了代码,但仍然是一样的。
正如在这线程中提到的,我尝试使用德利蒙的库,但是它在行中抛出了一个System.OverflowException。
string[] a = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);有人知道如何解决这个问题吗?(我不可能改变文件结构,使用映射驱动器也是不可能的)。
谢谢
编辑:
我加了
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false"/>
</runtime>到app.config (注意,它是一个控制台应用程序)。现在在行中抛出一个System.IO.FileNotFoundException
b += fi.Length;Edit2:
app.config文件如下所示:
<?xml version="1.0"?>
<configuration>
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false"/>
<runtime targetFramework="4.7"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
</runtime>
<appSettings>
<add key="SQLServer" value="Server2"/>
<add key="database" value="FolderSizeMonitor"/>
<add key="server" value="Server3"/>
</appSettings>
<startup>
<dir ID="1" path="\\server20\d$\Data\BEG\Emergency\Emergency Management\Very Very Very Long pathVery Very Very Long path" DepthToLook="4">
</dir>
</startup>
</configuration>发布于 2017-08-13 04:39:47
下面的代码实现了这个窍门:
static long getFolderSize(string path)
{
DirectoryInfo dirInfo = new DirectoryInfo(path);
long b = 0;
foreach(FileInfo fi in dirInfo.EnumerateFiles("*",SearchOption.AllDirectories))
{
b += fi.Length;
}
return b;
}https://stackoverflow.com/questions/45561890
复制相似问题