What is the longest file path allowed to be moved to Recycle Bin?

血红的双手。 提交于 2019-12-10 12:49:59

问题


For my application I'm testing the longest available file path to be allowed to be moved into the Recycle Bin and I'm getting interesting results.

On Windows XP the maximum size is 259 characters, which is the MAX_PATH constant minus 1.

But on my Windows 8.1 Pro, that maximum allowed file path size somehow seem to be 215 characters.

So I'm curious are there any official guidelines for this?

EDIT: OK, since posters below requested an API, I'm using SHFileOperation with FO_DELETE and FOF_ALLOWUNDO to place a user's file into the Recycle Bin. Since Windows Explorer uses the same exact API for its Delete operation it's easy to test it by making a long path within Windows Explorer and then by trying to delete it. In my experiments I can see the following:

  • Windows XP, if the total path length is 259 chars (on some editions, it may be 257??), the file/folder will be placed into the Recycle Bin. Otherwise Windows Explorer offers only an option to permanently delete it.

  • Windows Vista, this limit is 217 chars, inclusively.

  • Windows 7 and 8, it is 215 chars, inclusively.

So it seems like this maximum limit is shrinking... Thus I was just curious, if this is documented somewhere in MSDN?


回答1:


Recycle Bin internals:

Windows XP

Every drive has its own drive:\RECYCLER\%USER_SID% directory. This directory contains all deleted files but files have names like DcN.ext where D is fixed part of the name, c is drive letter, N is a index and ext is extension of original file. Besides deleted files there is database file named INFO2.

INFO2 file starts with header. Header structure:

Offset Type  Value
0x0000 DWORD Signature  ; Always 5
0x0004 DWORD Unknown1
0x0008 DWORD Unknown2
0x000C DWORD RecordSize ; Always 0x00000320
0x0010 DWORD Unknown3

Records are stored successively immediately after header to the end of the INFO2 file. Record structure:

Offset Type               Value
0x0000 ANSICHAR[MAX_PATH] OriginalFileNameA ; Ansi string
0x0104 DWORD              Index             ; Associated with *N* from *DcN.ext*
0x0108 DWORD              DriveIndex        ; A: = 0; B: = 1; C: = 2; ...
0x010C FILETIME           DeleteFileTime
0x0114 DWORD              OriginalFileNamePhysicalSize
0x0118 WIDECHAR[MAX_PATH] OriginalFileNameW ; Wide string

Windows Vista and above

Every drive has its own drive:\$Recycle.Bin\%USER_SID% directory. This directory contains all deleted files but now there is no database file. Every deleted file is associated with 2 files inside RB.

First file has name like $INNNNNN.ext where $I is fixed part of the name, NNNNNN consists of 6 random letters or numbers and ext is extension of original file.

$I file structure:

Offset Type               Value
0x0000 DWORD              Signature         ; Always 1
0x0004 DWORD              Unknown1
0x0008 DDWORD             OriginalFileSize
0x0010 FILETIME           DeleteFileTime
0x0018 WIDECHAR[MAX_PATH] OriginalFileNameW ; Wide string

Second file has name like $RNNNNNN.ext where $R is fixed part of the name, NNNNNN is the same as in $I file and ext is extension of original file. $R file is deleted file itself.

As you see in all cases Windows stores filename in array which has size of MAX_PATH chars. That why the limit of length of filename is MAX_PATH - 1 chars.

Windows 10

Windows 10 has a new version of $I file structure (don`t know what update changed it):

Offset Type                          Value
0x0000 DWORD                         Signature         ; Always 2
0x0004 DWORD                         Unknown1
0x0008 DDWORD                        OriginalFileSize
0x0010 FILETIME                      DeleteFileTime
0x0018 DWORD                         OriginalFileNameLen
0x001C WIDECHAR[OriginalFileNameLen] OriginalFileNameW ; Wide string

And it looks like now Windows can store any files with any paths in the Recycle Bin.




回答2:


Simple answer. The recycle bin is a (well) hidden folder on disk. All the files in it keep their original names, plus the extra length for the name of the recycled bin folder.

In Windows XP the path looked like this:

C:\RECYCLER\S-1-5-21-1089551744-1120685985-1162132538-1003\

I'm sure it's similar in later versions, but perhaps getting longer. Your filename has to go on on the end of that, and still stay inside the 260 limit.


So how do you find that name for a specific system? Try GetFinalPathnameByHandle, or this blog.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364962.aspx

http://pdh11.blogspot.com.au/2009/05/pathcanonicalize-versus-what-it-says-on.html


More useful places to look.

IKnownFolder: http://msdn.microsoft.com/en-us/library/windows/desktop/bb776912.aspx

SHGetKnownFolderPath: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188%28v=vs.85%29.aspx

SHGetFolderPath using CSIDL: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181%28v=vs.85%29.aspx



来源:https://stackoverflow.com/questions/23926114/what-is-the-longest-file-path-allowed-to-be-moved-to-recycle-bin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!