Why does the 260 character path length limit exist in Windows?

后端 未结 11 1121
孤城傲影
孤城傲影 2020-11-22 03:18

I have come up against this problem a few times at inopportune moments:

  • Trying to work on open source Java projects with deep paths
  • Storing deep Fitne
11条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 03:42

    The question is why does the limitation still exist. Surely modern Windows can increase the side of MAX_PATH to allow longer paths. Why has the limitation not been removed?

    • The reason it cannot be removed is that Windows promised it would never change.

    Through API contract, Windows has guaranteed all applications that the standard file APIs will never return a path longer than 260 characters.

    Consider the following correct code:

    WIN32_FIND_DATA findData;
    
    FindFirstFile("C:\Contoso\*", ref findData);
    

    Windows guaranteed my program that it would populate my WIN32_FIND_DATA structure:

    WIN32_FIND_DATA {
       DWORD    dwFileAttributes;
       FILETIME ftCreationTime;
       FILETIME ftLastAccessTime;
       FILETIME ftLastWriteTime;
       //...
       TCHAR    cFileName[MAX_PATH];
       //..
    }
    

    My application didn't declare the value of the constant MAX_PATH, the Windows API did. My application used that defined value.

    My structure is correctly defined, and only allocates 592 bytes total. That means that i am only able to receive a filename that is less than 260 characters. Windows promised me that if i wrote my application correctly, my application would continue to work in the future.

    If Windows were to allow filenames longer than 260 characters then my existing application (which used the correct API correctly) would fail.

    For anyone calling for Microsoft to change the MAX_PATH constant, they first need to ensure that no existing application fails. For example, i still own and use a Windows application that was written to run on Windows 3.11. It still runs on 64-bit Windows 10. That is what backwards compatibility gets you.

    Microsoft did create a way to use the full 32,768 path names; but they had to create a new API contract to do it. For one, you should use the Shell API to enumerate files (as not all files exist on a hard drive or network share).

    But they also have to not break existing user applications. The vast majority of applications do not use the shell api for file work. Everyone just calls FindFirstFile/FindNextFile and calls it a day.

提交回复
热议问题