Adam Hodovanets
Adam Hodovanets

Reputation: 223

Could not find the part of the path, but it exists

I created a method that returns size of folder.

public static long GetDirectorySize(DirectoryInfo d)
    {
        long size = 0;
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        foreach (FileInfo fi in fis)
        {
            size += fi.Length;
        }
        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        foreach (DirectoryInfo di in dis)
        {
            size += GetDirectorySize(di);
        }
        return size;
    }

It works for usual paths, but for() it throws an error: could not find the part of the path path in cmd

Upvotes: 2

Views: 13838

Answers (2)

Ali Rad
Ali Rad

Reputation: 88

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the \?\ prefix. For example, \?\D:\very long path.

Following post discuss about your topic well:

How to deal with files with a name longer than 259 characters?

Upvotes: 3

Gauravsa
Gauravsa

Reputation: 6528

I tried to emulate your problem:

string path = "C:\\_VSource\\VisoftApplication\\bin\\x64\\Debug\\Projekte\\Temporary\\KRUG_PETRA_WILFRIED_2\\Data\\TextureImages\\Custom\\Sanitärobjekte\\_textures\\wc-vorwand 2,025 mit nische\\Standard\\sanitary\\visoft_dekoration515\\sanitary\\visoft_dekoration515\\visoft_dekoration\\textures";

if (!Directory.Exists(path))
    Directory.CreateDirectory(path); // received an error here

Error is this one:

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

You can have a look here for a.Net library that works with long paths Zeta Long Paths

Upvotes: 1

Related Questions