I have the following code:
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;
}
In the environment where this code is running, there are paths that exceed the 260 character limit. Therefore the line
FileInfo fi = new FileInfor(name);
throws a System.IO.PathTooLongException.
I've read a lot about this and according to https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/ the problem should be solved in .NET 4.6.2. Therefore I've compiled the code in .NET 4.7 but still the same thing.
As mentioned in this thread I've tried to use Delimon's Library but it throws a System.OverflowException in the line
string[] a = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
Does anybody have a clue how tho solve this issue? (There is no way I change the file structure and using mapped drives is also no possibility).
Thanks
EDIT:
I added
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false"/>
</runtime>
To the app.config (note that it is a console application). Now there is a System.IO.FileNotFoundException thrown in the line
b += fi.Length;
Edit2:
This is how the app.config file looks like:
<?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>