0

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>
josibu
  • 594
  • 1
  • 6
  • 23
  • Possible duplicate of [.NET System.IO.PathTooLongException from Web Application](https://stackoverflow.com/questions/45476852/net-system-io-pathtoolongexception-from-web-application) – Cleptus Aug 08 '17 at 07:32
  • See edit. I am running Windows 10 (latest release) and .NET 4.7 installed. – josibu Aug 08 '17 at 07:59
  • In the linked question, the problem was that it needed to be enforced the framework version in the .config file. Maybe it helps you. – Cleptus Aug 08 '17 at 08:40
  • I do have the supportedRuntime tag in the app.config. Isn't that enough? If not, what is the equivalent for httpRuntime in Console Application? – josibu Aug 08 '17 at 09:08
  • should be enough, according to https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-configure-an-app-to-support-net-framework-4-or-4-5 – Cleptus Aug 08 '17 at 09:39
  • still doesn't work though – josibu Aug 08 '17 at 11:17

1 Answers1

0

The following code did the trick:

    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;
    }
josibu
  • 594
  • 1
  • 6
  • 23