Shell32 in service

nofrills

Member
Joined
Oct 14, 2015
Messages
12
Programming Experience
3-5
I tried to take this a step further and create a service for this instead of the windows application. I found my lovely users were closing out this program once it started.... why???!? Because they are users and do everything they shouldn't.

But like everything else with Recycle Bin, I am finding that Shell32 doesn't seem to work the sam eway in a service, as from what I read, it is meant to be a UI, something a service doesn't support. But nothing I have tried thus far seems to read the recycle bin folders correctly. I have been trying FileSystems and IO scripts, but can't seem to get anything displaying exactly what i have in the Recycle bin. My brain is so fried from searching the net.

Anybody have any experience with dealing with Window Services and Recycle Bin's?
 
Last edited by a moderator:
Because they are users and do everything they shouldn't.
They do whatever they are in their rights to do :)
But like everything else with Recycle Bin, I am finding that Shell32 doesn't seem to work the sam eway in a service, as from what I read, it is meant to be a UI, something a service doesn't support.
No problem using Shell32 in a windows service, but it has to run under a user account to get access to recycle bin of that user. If it needs to work for multiple user accounts on one machine
a windows service may not be a good solution here.
 
I will attempt again, but i moved the code that was working from you into the service and it seem to cause an exception error (need to double check). Off hand, I don't recall the exact message, but It might had to do with an object needed to be created. But if a user is logged onto the Server, wouldnt the service be automatically assigned to their account/recycle bin. Or do I need to provide some type of logic in the code to have the current users credential controlling the permission for Shell32?

Thank you!
 
No, you set service log on to a specific account. Services has otherwise nothing to do with any interactive user that may be logged in to the machine.
 
Thank you. I will research this and see if my results are any different. This is my watered down code just to see if I can successfully read the Recycle Bin. I have roughly a dozen entries in the bin, but the code here only display 3 of them? All the JPGS I have there. - And Why the G Drive, it should be C ???

This is from the Event Log: G:\$RECYCLE.BIN\$R6I1T11.jpg ( I do have an external Drive G)



Public Class Service1




Protected Overrides Sub OnStart(ByVal args() As String)
Me.EventLog.WriteEntry("CopyRecycleService has started.")


Try
Me.EventLog.WriteEntry("Checking Files")


Dim Filename1 As String
Dim Filename2 As Date
Dim SH = CreateObject("Shell.Application")
Const ssfBITBUCKET = 10
Dim RecycleBin As Object = SH.NameSpace(ssfBITBUCKET)
Me.EventLog.WriteEntry("Created Shell")






For Each Item As Object In RecycleBin.Items


Me.EventLog.WriteEntry(Item.path)
Me.EventLog.WriteEntry(Item.type)


If (Item.Type) = "DAT File" Or (Item.Type) = "PH1 File" Then




Me.EventLog.WriteEntry("Found Files- Moving")
Filename2 = Item.ModifyDate
Filename1 = Filename2.ToString("MMddyyyy")


Me.EventLog.WriteEntry("Moving Files now.")
Me.EventLog.WriteEntry("c:\archive" & Filename1 & "" & Item.Name)
If (Not System.IO.File.Exists("c:\archive" & Filename1 & "" & Item.Name)) Then
System.IO.File.Copy(Item.Path,
"c:\archive" & Filename1 & "" & Item.Name)
Else


End If
End If




Next Item
Catch ex As Exception
Me.EventLog.WriteEntry(ex.ToString)


End Try
End Sub


Protected Overrides Sub OnStop()
Me.EventLog.WriteEntry("CopyRecycleService has stop.")


End Sub
 
Hmm ok, after setting the Service with my account information, it now does seem to pull everything in my Recycle Bin - Is there a way for the Service to automatically use the Login Credentials of the user who is logged in to start the service, or can a Admin account be programmed into the code. Would the later still allow the copying of the users recycling bin if they are logged on, even though a different account is being used to run the service ?


Thank you in advance - this has been very educational!
 
Is there a way for the Service to automatically use the Login Credentials of the user who is logged in to start the service
No. Services operate independently and separated from interactive user logins.
the user who is logged in
Services run even if there are no users logged in. Services that is set to start automatically start before any user logs in. They continue to run when users log out. Multiple users can be logged in to a machine at the same time.
can a Admin account be programmed into the code
What? A service can run under any user account that has 'log on as service' permission. Some operations would require a service to run as an admin or a system account.
copying of the users recycling bin if they are logged on, even though a different account is being used to run the service ?
It can be achieved through impersonation, but you also need to load the user profile into impersonation context to access the recycle bin of the impersonated user. Start a new thread about this new topic if you encounter problems with this during your research. Impersonation is something you can do in any project, it doesn't have to be a service. I warn you this is a highly advanced topic.
 
Back
Top