Results 1 to 7 of 7

Thread: Shell32 in service

  1. #1
    nofrills is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2015
    Posts
    12
    Reputation
    18

    Shell32 in service

    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 JohnH; 10-23-2015 at 3:17 AM. Reason: thread split

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,069
    Reputation
    2801
    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.

  3. #3
    nofrills is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2015
    Posts
    12
    Reputation
    18
    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!

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,069
    Reputation
    2801
    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.

  5. #5
    nofrills is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2015
    Posts
    12
    Reputation
    18
    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

  6. #6
    nofrills is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2015
    Posts
    12
    Reputation
    18
    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!

  7. #7
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,069
    Reputation
    2801
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •