5

I'm using Python but I think the same problem exists in C#.

I'd like to be able to support extended paths (paths with len > 260 which are represented like //?/c:/blah/blah). It looks like the standard way to move files to the recycle bin is via SHFileOperationW or SHFileOperation.

In python the helpful xplatform library, move2trash, uses SHFileOperationW. A few articles on C# say to use the same thing.

The problem with SHFileOperationW is that it doesn't support extended paths. This is a general limitation of the SH tools.

Does anyone have any tricks for dealing with the recycle bin and extended paths?

reference links: C# version, Python version

Community
  • 1
  • 1
greenhat
  • 1,061
  • 1
  • 12
  • 19
  • 1
    I just found this stackoverflow question, http://stackoverflow.com/questions/11737109/how-can-i-delete-a-folder-or-file-with-path-too-long-in-recycle-bin. Investigating to see if it holds the answer. – greenhat Feb 27 '13 at 00:38
  • This is for Windows, right? – Eric O. Lebigot Feb 27 '13 at 00:53
  • The same problem would exist for any language that uses the Win32API actually. (In light of that it might be better to use [tag:language-agnostic] instead of random language tags.) – millimoose Feb 27 '13 at 01:36
  • Can you require Vista or later? Then you can use [`IFileOperation`](http://msdn.microsoft.com/en-us/library/bb775771(v=vs.85).aspx), which will work on anything you can construct an `IShellItem` for. This might just result in a more useful error, rather than success, but it's worth trying. – abarnert Feb 27 '13 at 01:47
  • Also, IIRC, the way the Vista Explorer handles this is to actually rename the file to its "path-squeezed" equivalent (see [here](http://blogs.msdn.com/b/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx) for some background) before trashing it. (The XP explorer doesn't have to do this, because it won't even let you see the files in the first place…) You can do much the same thing more simply (even in XP) with `GetShortPathName`. – abarnert Feb 27 '13 at 01:50

2 Answers2

0

A quick search for "PathTooLongException" turns up quite a few discussions on Stack Overflow about it if you want to have a look at some C# solutions. It might be possible for you to retrieve the "short name" and use that to move it. Also, you could look and see how they've handled it in this C# library with a workaround at http://bcl.codeplex.com/wikipage?title=Long%20Path

Tim
  • 857
  • 6
  • 13
0

You don't. It is not possible to put a file in the recycle bin that's path is longer than 259 characters.

  • 1
    I don't think that's quite true, because you can definitely do it in Vista's Explorer. IIRC, the way this works is by using the same path-squeezing tricks that let you see the file in Explorer in the first place. – abarnert Feb 27 '13 at 01:51