Archive: Need to store text files in AppData folder


Need to store text files in AppData folder
Now that I am in Windows 7, I suddenly find that simple text files that my app updates per user are denied access. Until now, I have kept these profiles in a subfolder of my install directory.

I am wondering if I could get a bit of direction on how to use NSIS to find the right appdata folder and place some files in that location during installation. Of course, the folder name varies, such as 'Application Data' in XP and 'AppData' in Windows 7. I am thinking and hoping that this move will eliminate access problems.


Look up $APPDATA in the wiki.
I think that's what you want. There is a warning about how the SetShellVarContext is set.


It's all in the manual.

http://nsis.sourceforge.net/Docs/Chapter4.html#4.2.3


Thank you, gentlemen. I did discover $APPDATA after posting, but this is a new concept for me.

I am wondering: When might "All Users" be chosen over "Current user"?


If you enforce admin access for your installer (for example because you need to write to a protected directory such as $SYSTEM or $PROGRAMFILES), you must install for all users. In this case, you must use the HKLM registry hive for all registry stuff.

If you do not do anything that requires admin access, you can do one of two things:
1) Force admin-only installation anyway. This means installing for all users and using HKLM (see above).
2) Allow user-level installation. This means NOT asking for admin access, NOT writing to any protected folders, only installing for the current user, and using the HKCU registry hive instead of HKLM.

This is how windows is designed to operate, so this is how you're supposed to do it.


Originally posted by SteveRussell
I am wondering: When might "All Users" be chosen over "Current user"?
I you have things that are shared with all users of your program

Originally posted by MSG
If you enforce admin access for your installer (for example because you need to write to a protected directory such as $SYSTEM or $PROGRAMFILES), you must install for all users.
I definitely do write to program files. But I thought that it was no longer possible to enforce admin access with RequestExecutionLevel.

You need RequestExecutionLevel admin but you still need to check the user is an administrator in .onInit and abort if they are not.

Stu


Originally posted by Afrow UK
You need RequestExecutionLevel admin but you still need to check the user is an administrator in .onInit and abort if they are not.

Stu
I would love to see an example of checking the user. Haven't found one yet.

requestexecutionlevel doesn't do anything in WinXP, or in Win7 if UAC is turned off. So use both:

requestexecutionlevel admin

function .onInit
userInfo::getAccountType
pop $0
${If} $0 != "Admin"
messageBox MB_OK "Requires admin"
quit
${EndIf}
functionend

(and same for un.onInit!)


(Ignore this post. I had inserted code before includes.):

Thank you. I'm getting an error:

Function: ".onInit"
File: "UserInfo.dll"->"$PLUGINSDIR\UserInfo.dll" [compress] 1336/4096 bytes
Plugin Command: GetAccountType
Pop: $0
Invalid command: ${If}

------

Function .onInit
UserInfo::GetAccountType
Pop $0
${If} $0 != "Admin"
MessageBox MB_OK "Your system requires administrative permissions in order to install this software."
Quit
${EndIf}
FunctionEnd


!include LogicLib.nsh


Should I remove related folders and data files from the appdata folder when uninstalling?


Depends what it is. If you uninstall word do you expect the .doc files to be deleted.
I don't know if that analogy works in your case.


You should delete everything you don't want to keep. As simple as that.


I kind of like the doc comparison. My tiny data files are profiles that record user progress, otherwise meaningless when the application is uninstalled. But they could remain for re-installation.

I have never before dealt programmatically with the appdata file structure, so never had to think about some of these ramifications. I treat the register very carefully, leaving it clean at uninstall. Now I am wondering about the neighborhood rules - Is it considered sloppy to allow some files to remain in appdata when the program is removed?


Originally posted by SteveRussell
Is it considered sloppy to allow some files to remain in appdata when the program is removed?
That entirely depends on your definition. "Uninstallation" implies you undo the installation, but you can define "installation" as that which the installer created (so, only the original application), or you can define it as everything that comprises and belongs to the installed application (so, both the app and its settings etc).

Note that it may also depend on the size and number of files you're leaving behind. What most uninstallers do is add a checkbox or messagebox option to delete saves/settings along with the application.

You should ask the user if they wish the files to be deleted. You can either use a simple MessageBox, and an uninstall component (i.e. another un. Section) or add a custom page with a check-box on it.

Stu


Yes, I need to give the user the choice to delete or keep the associated files. Thank you all again for your thoughts, which have clarified mine. I greatly appreciate your advice and direction.


Originally posted by Afrow UK
You can either use a simple MessageBox, and an uninstall component (i.e. another un. Section)....

Stu
Section, or Function?

You can have multiple uninstall sections along with an uninstall components page (read the docs).

Stu


OK, I have created un.CleanUp_Profiles as an additional Section.

Section un.CleanUp_Profiles
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Do you want to delete all Sign In id's associated with $(^Name)?" IDYES true IDNO false
true:
Delete "$APPDATA\${PRODUCT_FAMILY}\pf\*_scii.txtt"
Goto next
false:
Goto next
next:
SectionEnd

I was already conditionally deleting other files and folders in Section Uninstall, depending on whether or not other similar products have been installed in the same directory.

I don't see the docs telling me what should go inside the main section and what should not.


For NSIS it doesn't matter in which section you put it. That's entirely up to you. If you want to make it an optional section, obviously all the optional files should be deleted in that section, not in another.