1

I've got a piece of code that calls the DeleteFile method in the Microsoft.VisualBasic.FileIO.FileSystem class (in the Microsoft.VisualBasic assembly) in order to send the file to the Recycle Bin instead of permanently deleting it. This code is in a managed Windows Service and runs on a Win Server 2k8 machine (32-bit).

The relevant line:

FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.DoNothing);

Of course, I've got "using Microsoft.VisualBasic.FileIO;" at the top of the class and I verified that the method being called is really on the FileSystem class in that namespace. In the above line I refer to a local variable "file" - that's a FileInfo for a local file (say, C:\path\to\file.txt) of which I'm certain that it exists. The application has Full Control over both the file and the directory it's in.

This appears to work just fine as the file disappears from the directory it was in. However, the file doesn't show up in the Recycle Bin. I tried inspecting the C:\$Recycle.Bin folders manually as I suspected the Windows Service running in session 0 would make it end up in a different Recycle Bin, but all the Recycle Bins appear empty.

Does anybody have a clue as to what causes this behavior?

By the way - the machine is definitely not out of free space on the drive in question (or any other drive for that matter), and the file is very small (a couple of kilobytes, so it doesn't exceed the Recycle Bin threshold).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mels
  • 476
  • 2
  • 17

2 Answers2

5

I assume your service is running under a different user account than your own (or one of the special service accounts).

I don't believe it's possible for one user to view the contents of another user's recycle bin - even though you can see some evidence of their existence within the C:\$Recycle.Bin folder.


If it's running under another user account, try logging into the machine using that account, and then check the recycle bin. If it's running under a service account (e.g. Local Service, Network Service, or Local System) it's going to be trickier.

Given that the recycle bins are separate, how are you planning to make use of the fact that the file is in the recycle bin anyway?

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • The service runs as Local System. I'll try what happens when I change the process identity to an account I can log into interactively. – Mels Jan 10 '11 at 14:33
  • I didn't know that there is absolutely no way to access another user's Recycle Bin. I think I actually did that once in the past, but it might be that I'm mistaken or the isolation has since become stronger. I'll investigate further. – Mels Jan 10 '11 at 14:36
  • @Mels - I understand (from the great trusted source that is wikipedia) that it changed between 2000 and XP. But it's "citation needed". – Damien_The_Unbeliever Jan 10 '11 at 14:37
  • Just tried executing the service under a new account with appropriate rights. Same result: file is gone, but doesn't appear in any recycle bin. It appears as though services just can't send files to the recycle bin somehow. I guess I'll just move the files to a custom "recycle bin" folder instead of deleting them, unless anyone has any better ideas? – Mels Jan 10 '11 at 15:31
  • @Mels - that would seem reasonable if you expect others to be able to retrieve these files. – Damien_The_Unbeliever Jan 10 '11 at 15:33
  • I think the shell api requires an interactive session to work properly especially for querying special folders which is critical in that case. – dvhh Jan 10 '11 at 17:03
0

the problem could come from the user executing your service, could you try to alter the executing user policy or change the executing user.

Anyway, it could also come from the service being executed without a shell, as the recycle bin depend on the shell api. this post seem to confirm this issue. So you would need to take another approach to acces shell api from your service.

Community
  • 1
  • 1
dvhh
  • 4,724
  • 27
  • 33