Deleting files in APPDATA for all users - Advanced Installer Community
rho
Posts: 69
Joined: Tue May 12, 2009 1:22 pm

Deleting files in APPDATA for all users

Hi,

my program creates copies of master config files for each user in %APPDATA%\myfolder at runtime. To prevent any issues with outdated config files, I'd like to delete them for any user on uninstall or upgrade.
As %APPDATA% resolves to the current user, I currently don't know how to delete "myfolder" for other users which were running the software on this machine.
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Deleting files in APPDATA for all users

Hello,

You can add conditioned file operations in the Application Data folder from the Files and Folders page.
Here's how to detect an upgrade.

All the best,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
rho
Posts: 69
Joined: Tue May 12, 2009 1:22 pm

Re: Deleting files in APPDATA for all users

Thanks for your answer.
I wonder if the links you provided will solve this issue, because:

%APPDATA% resolves to something different for each user. I.e.:
C:\Users\<username1>\AppData\Roaming
C:\Users\<username2>\AppData\Roaming
...

To use the File Operations from AI I would need to set a mask like
"C:\Users\*\AppData\Roaming\myfolder "
to delete it. Is this supported?
To stay compatible with XP, I'd need a variable/property for the part in front of the username:
"C:\Documents and Settings\<username>\Application Data" on XP -> C:\Documents and Settings\
"C:\Users\<username>\AppData\Roaming" on Vista or above -> C:\Users\
Furthermore this path is at least on XP different on a non english windows.

For now I adapted a custom action to solve the path issue and to iterate over the folders in C:\Users\ (or C:\Documents and Settings\ on XP) to get each user and check if there is a folder from my application.
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Deleting files in APPDATA for all users

Hello,

The Application Data folder in Advanced Installer is resolved accordingly for each OS type.
To remove an entire folder, you can also look into the folder Properties from its context menu -> Removal tab functionality.

All the best,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
rho
Posts: 69
Joined: Tue May 12, 2009 1:22 pm

Re: Deleting files in APPDATA for all users

I think I'm still not clear.

I want to remove "myFolder" for EACH USER who has ever logged on to the current machine. There is a "myFolder" for each user in his own %APPDATA% folder.
To put in a cleanup rule for %APPDATA%\myFolder won't affect any of the other users on the machine. It will do the cleanup just for the currently logged on user. Correct me if I'm wrong.
mihai.petcu
Posts: 3860
Joined: Thu Aug 05, 2010 8:01 am

Re: Deleting files in APPDATA for all users

Hi,
I want to remove "myFolder" for EACH USER who has ever logged on to the current machine. There is a "myFolder" for each user in his own %APPDATA% folder.
I had missed this part. Please keep in mind this is not a correct or recommended functionality for your application.
The application should either create files in a per machine location like Common Application Data folder:

Code: Select all

C:\Documents and Settings\All Users\Application Data

Code: Select all

C:\ProgramData
or in a per user location like Application Data folder:

Code: Select all

C:\Documents and Settings\<username>\Application Data

Code: Select all

C:\Users\<username>\AppData\Roaming
Since this is an unusual scenario I'm afraid there's no predefined functionality to uninstall the files in Advanced Installer.

All the best,
Mihai
Mihai Petcu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
rho
Posts: 69
Joined: Tue May 12, 2009 1:22 pm

Re: Deleting files in APPDATA for all users

Mihai,

%APPDATA% is a windows system variable and resolves to just these folders you mentioned:
C:\Documents and Settings\<username>\Application Data
or
C:\Users\<username>\AppData\Roaming

so I think my approach using "%APPDATA%\myfolder" is correct.
But nevermind, I've a working solution for now.
ysimeoni
Posts: 3
Joined: Thu Oct 25, 2012 10:22 am

Re: Deleting files in APPDATA for all users

Hello,

I've same issue.
Our soft is installed for all user but each users that use the soft has a settings folder created in their %APPDATA%. We need to delete it at removal.
rho wrote: For now I adapted a custom action to solve the path issue and to iterate over the folders in C:\Users\ (or C:\Documents and Settings\ on XP) to get each user and check if there is a folder from my application.
Is it possible to get the code of your custom action ?

Thanks a lot.
rho
Posts: 69
Joined: Tue May 12, 2009 1:22 pm

Re: Deleting files in APPDATA for all users

Sorry, I can't give you the final dll, but the working part of it.

My solution consists of 3 parts:
1. I use a custom action to call a c# dll which I pass a maintenance flag.
2. the flag is a populated in a custom action, too. (property set with formatted). For me it is always needed.
in execution stage condition, everything is checked and I put this code into the Condition field, to get maintenance and MSPs, too:

Code: Select all

MSIPATCHREMOVE OR AI_PATCH OR PATCH OR REINSTALL OR AI_MAINT

3. in C# it is the default implementation needed for AI among other things I need to do. The functional part boils down to:
- build a pattern for the appdata path for xp or win 7
- iterate all users in appdata and delete the folder you are looking for.

Code: Select all

        /// <summary>
        /// deletes the config files from %APPDATA%\MyFolder for each user on the machine
        /// </summary>
        public void DeleteAppDataFiles()
        {
            string baseUserFolder, templateAppdata;
            GetAppDataMyFolderPath(out baseUserFolder, out templateAppdata);

            var users = Directory.GetDirectories(baseUserFolder);
            foreach (var userPath in users)
            {
                try
                {
                    string myFolderPath = userPath + templateAppdata + "\\MyFolder"; 
                    if (Directory.Exists(myFolderPath))
                    {
                        Directory.Delete(myFolderPath, true);
                    }
                }
                catch (Exception ex)
                {
                    Logging.WriteLogEntry(System.Diagnostics.EventLogEntryType.Error, ex.ToString());
                }
            }
        }

        private void GetAppDataMyFolderPath(out string baseUserFolder, out string templateForAppDataPath)
        {
            string appdataFolder = Environment.ExpandEnvironmentVariables("%APPDATA%");
            // results in
            // "C:\Documents and Settings\<username>\Application Data" on XP
            // "C:\Users\<username>\AppData\Roaming" on Vista or above 

            //from start to second \
            int scndBackslash = appdataFolder.IndexOf("\\", 5, StringComparison.Ordinal);
            baseUserFolder = appdataFolder.Substring(0, scndBackslash);
            //get rid of C:\Users\<username>
            //cant use Environment.UserName to replace, as it may be SYSTEM while in  %APPDATA% is current user
            templateForAppDataPath = appdataFolder.Substring(
                appdataFolder.IndexOf("\\", scndBackslash + 1, StringComparison.Ordinal));//start at third \
        }
hope it helps :)
ysimeoni
Posts: 3
Joined: Thu Oct 25, 2012 10:22 am

Re: Deleting files in APPDATA for all users

Thank you very much.

It is working on my computer (thanks to .NET).

Our target platform might not have .NET, that's why I will write the DLL in C++.

Thanks again
superprat
Posts: 1
Joined: Thu Feb 06, 2014 5:04 pm

Re: Deleting files in APPDATA for all users

Increment the version number of your application. It creates a separate settings file in Appdata. There are subdirectories there based on the version number.

Return to “Common Problems”