Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a tool in vb/windows forms that will delete user profiles on remote computers. It works great except for displaying the computer names and the profiles that were deleted.

What I have tried:

When I try ObjectListView it displays the information on the first row and the overwrites it for each computer and each profile deleted on each computer. Here is the part of the code I am having issues with....

VB
Public Class PC
        Public Property Computer_Name As String
        Public Property Profile_Name As String
    End Class

For Each objProfile In colProfiles
                        Dim dtmLastUseTime = CDate(Mid(objProfile.LastUseTime, 5, 2) & "/" &
        Mid(objProfile.LastUseTime, 7, 2) & "/" & VB.Left(objProfile.LastUseTime, 4) _
         & " " & Mid(objProfile.LastUseTime, 9, 2) & ":" &
             Mid(objProfile.LastUseTime, 11, 2) & ":" & Mid(objProfile.LastUseTime,
                 13, 2))
                        If DateDiff("d", dtmLastUseTime, d1) > intMaxProfileAge Then
                            usrPath = objProfile.localpath
                            rowInfo2 = usrPath

                            Dim LvItm As New PC With {.Computer_Name = rowInfo1,
                              .Profile_Name = rowInfo2}
                            Dim LvLst As New List(Of PC)
                            LvLst.Add(LvItm)
                            ObjectListView1.SetObjects(LvLst)
                            
                            objProfile.Delete_
                        End If
                    Next
Posted
Updated 29-Jun-17 10:09am

1 solution

If you can't see the problem by looking at your code, then you should debug it. When you do, the problem will become obvious: you're creating a new, empty list on each iteration of your loop, and only ever adding a single item to it.

You need to create the list outside of the loop:
Dim LvLst As New List(Of PC)

For Each objProfile In colProfiles
    Dim dtLastUseTime = ...
    If DateDiff("d", dtmLastUseTime, d1) > intMaxProfileAge Then
        
        Dim LvItm As New PC With { .Computer_Name = rowInfo1,
                              .Profile_Name = objProfile.localpath }
        
        LvLst.Add(LvItm)
        
        objProfile.Delete_
    End If
Next

ObjectListView1.SetObjects(LvLst)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900