GetProfilesDirectory() aka get all users and appdata folders

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

GetProfilesDirectory() aka get all users and appdata folders

Post by Rescator »

Not much of a trick, more like a tip really.

What is the point of this code?

Well imagine that an app is installed for All Users,
now when the admin uninstall the app it's start menu and so on icons will be removed.

But most likely every user that has used the app had their own preferences saved, the standard (and recommended place) to save preferences etc. on vista and later is in \AppData\Roaming\

A normal uninstaller may be able to remove the settings for the current user (usually the admin for example) but not for all other users.

This little source shows you how to list all user profile folders and their appdata folder paths both for Vista and for older Windows. (anything older than Windows 2000 not supported)

Hopefully this will help make it easier for some of you to list all user's appdata\roaming\ folders, check and then remove your apps settings folder and files, thus leaving a clean system behind.

Remember, the ideal app leaves the OS in the same state as it was before it was installed.

PS! It only makes sense to offer "Delete program settings from all users?"
if the app was installed for "all users", if the app was installed for the current user. (i.e. just the admin) then this would make no sense obviously.

Code: Select all

EnableExplicit
;windows 2000 or later required.

Procedure.s GetApplicationDataDirectory()
 Protected path$
 path$=Space(#MAX_PATH*2)
 If SHGetSpecialFolderPath_(#Null,@path$,#CSIDL_APPDATA,#True)
  Trim(path$)
  If Right(path$,1)<>"\"
   path$+"\"
  EndIf
 Else
  path$=""
 EndIf
 ProcedureReturn path$
EndProcedure

Procedure.s GetProfilesDirectory()
 Protected path$,len.i
 If GetProfilesDirectory_(#Null,@len)=#False
  path$=Space(len)
  If GetProfilesDirectory_(path$,@len)
   If Right(path$,1)<>"\"
    path$+"\"
   EndIf
  Else
   path$=""
  EndIf
 EndIf
 ProcedureReturn path$
EndProcedure

;all userpaths for appdata
Define path$,len.i,user$
path$=GetProfilesDirectory()
If path$<>""
 If ExamineDirectory(0,path$,"*.*")  
   While NextDirectoryEntry(0)
    If DirectoryEntryType(0)=#PB_DirectoryEntry_Directory
     user$=DirectoryEntryName(0)
     If (user$<>".") And (user$<>"..")
      If OSVersion()>=#PB_OS_Windows_Vista ;for Vista and later.
       Debug #DQUOTE$+path$+user$+"\AppData\Roaming\"+#DQUOTE$ ;this is what we want.
      Else ;for 2000/XP/etc.
       Debug #DQUOTE$+path$+user$+"\Application Data\"+#DQUOTE$ ;this is what we want.
      EndIf
     Else
      Continue ;we are skipping current and parent "folders".
     EndIf
    Else
     ;not a directory, this is a file instead.
    EndIf
   Wend
   FinishDirectory(0)
 Else
  ;unable to examine directory
 EndIf
Else
 ;unable to get path
EndIf


Debug ""
;current user appdata path only
Debug "Current User appdata path: "+GetApplicationDataDirectory()
User avatar
idle
Always Here
Always Here
Posts: 5918
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

Good tip, thanks
Post Reply