Only offer all user cleaning if the user is logged on as a local administrator.
Private Declare Function IsNTAdmin Lib "advpack.dll" (ByVal dwReserved As Long, ByRef lpdwReserved As Long) As LongPublic Function IsAdmin() as BooleanIsAdmin = CBool(IsNTAdmin(ByVal 0&, ByVal 0&))End Function
If cleaning fails because of NTFS file permissions denying the local administrator rights, then so be it. But have some form of notification to say that it failed.
Ok, I take your point about logged on users:
Option ExplicitPrivate Const HKEY_LOCAL_MACHINE = &H80000002Private Protected() As StringPrivate NumProtected As IntegerPrivate Users() As StringPrivate NumUsers As IntegerPublic Function GetLoggedOnUsers() As BooleanOn Error GoTo ErrorOcc? ?Dim objWMIService, objSystemSet, System, iTemp As Integer, sTemp As String? ?NumUsers = 0? ?Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")? ?Set objSystemSet = objWMIService.InstancesOf("Win32_ComputerSystem")? ?For Each System In objSystemSet? ? ? ?sTemp = System.UserName? ? ? ?iTemp = InStrRev(sTemp, "\")? ? ? ?AddUser LCase$(Mid$(sTemp, iTemp + 1, Len(sTemp) - iTemp))? ?Next? ?GetLoggedOnUsers = True? ?Exit FunctionErrorOcc:? ?GetLoggedOnUsers = FalseEnd FunctionPrivate Sub AddUser(ByVal UserName As String)? ?If NumUsers = 0 Then? ? ? ?NumUsers = 1? ? ? ?ReDim Users(1 To NumUsers) As String? ?Else? ? ? ?NumUsers = NumUsers + 1? ? ? ?ReDim Preserve Users(1 To NumUsers) As String? ?End If? ?Users(NumUsers) = LCase$(UserName)End SubPrivate Function CheckUse(ByVal sValue As String) As Boolean? ?Dim Count As Integer? ?sValue = LCase$(sValue)? ?For Count = 1 To NumUsers? ? ? ?If sValue Like Users(Count) & ".*" Or sValue = Users(Count) Then? ? ? ? ? ?CheckUse = True? ? ? ? ? ?Exit For? ? ? ?End If? ?Next CountEnd FunctionPrivate Sub AddProtected(ByVal FolderName As String)? ?If NumProtected = 0 Then? ? ? ?NumProtected = 1? ? ? ?ReDim Protected(1 To NumProtected) As String? ?Else? ? ? ?NumProtected = NumProtected + 1? ? ? ?ReDim Preserve Protected(1 To NumProtected) As String? ?End If? ?Protected(NumProtected) = LCase$(FolderName)End SubPrivate Function IsProtected(ByVal FolderName As String) As Boolean? ?Dim Count As Integer? ?FolderName = LCase$(FolderName)? ?For Count = 1 To NumProtected? ? ? ?If Protected(Count) = FolderName Then? ? ? ? ? ?IsProtected = True? ? ? ? ? ?Exit For? ? ? ?End If? ?Next CountEnd FunctionPublic Function EnumProfiles(ByRef ProfilesDir As String) As BooleanOn Error GoTo ErrorOcc? ?Dim objRegistry, arrSubkeys, objSubkey, strKeyPath As String, strSubPath As String, strValue As String, iTemp As Integer, sUser As String? ?NumProtected = 0? ?Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")? ?strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"? ?If objRegistry.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strKeyPath, "ProfilesDirectory", ProfilesDir) <> 0 Then GoTo ErrorOcc? ?If objRegistry.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath, "AllUsersProfile", strValue) <> 0 Then GoTo ErrorOcc? ?AddProtected strValue? ?'AddProfilePath ProfilesDir & "\" & strValue? ?If objRegistry.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath, "DefaultUserProfile", strValue) <> 0 Then GoTo ErrorOcc? ?AddProtected strValue? ?'AddProfilePath ProfilesDir & "\" & strValue? ?If objRegistry.EnumKey(HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys) <> 0 Then GoTo ErrorOcc? ?? ?'This function should fail if we have not managed to read the above values.? ?? ?For Each objSubkey In arrSubkeys? ? ? ?'Get the right subkey.? ? ? ?strSubPath = strKeyPath & "\" & objSubkey? ? ? ?? ? ? ?If GetStrExVal(strSubPath, "ProfileImagePath", strValue) = True Then? ? ? ? ? ?'Get the user stub.? ? ? ? ? ?iTemp = InStrRev(strValue, "\")? ? ? ? ? ?sUser = Mid$(strValue, iTemp + 1, Len(strValue) - iTemp)? ? ? ? ? ?If CheckUse(sUser) = True Then? ? ? ? ? ? ? ?'AddProfilePath strValue? ? ? ? ? ? ? ?AddProtected sUser? ? ? ? ? ?End If? ? ? ?End If? ? ? ?? ?Next? ?EnumProfiles = True? ?Exit FunctionErrorOcc:? ?EnumProfiles = FalseEnd FunctionPrivate Function GetStrExVal(ByVal strKeyPath As String, ByVal strValueName As String, ByRef strReturn As String) As Boolean? ?On Error GoTo ErrorOcc? ?Dim objRegistry? ?Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")? ?If objRegistry.GetExpandedStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strReturn) = 0 Then? ? ? ?GetStrExVal = True? ?Else? ? ? ?GetStrExVal = False? ?End If? ?Exit FunctionErrorOcc:? ?GetStrExVal = FalseEnd Function
I'll also code a running processes check to enumerate all user contexts when I can get around to it...
I'll have to think about the NTUSER.DAT and get back to you...