Page 1 of 1

Where to save user data, per OS?

Posted: Fri Aug 12, 2011 5:42 pm
by kenmo
Lately I've been getting into cross-platform programming, after writing exclusively for Windows for years. In fact I typically write for my own home computer, with Windows XP Pro, in which I have full admin privileges.

But for Vista/Win7 and Mac and Linux, I am wondering if I will run into problems with where to save program files. Usually I just save them into my program's current directory, or a subdirectory.

On Vista/Win7, isn't Windows more strict about where you are allowed to save files? (Maybe just non-admins?) Is it okay to save in the program's own folder, or should I play it safe and only save to GetHomeDirectory() + "\whatever"? And what should "whatever" be, "Application Data\MyProgramName", "Local Settings\AppData\MyProgramName", etc.?

The same questions apply to Mac/Linux. I have heard about programs failing App Store approval because they write to "Documents/MyProgramName" rather than "Documents/AppData/MyProgramName" or similar.

This typically applies to files unseen by the user (like preference files). Hopefully I don't have to worrying about the user selecting a disallowed folder to save user-created files, like images in an image editor?

Re: Where to save user data, per OS?

Posted: Fri Aug 12, 2011 6:20 pm
by ts-soft
GetHomeDirectory
You can use this on all OS, but typically is on windows APPDATA used.

Re: Where to save user data, per OS?

Posted: Fri Aug 12, 2011 6:21 pm
by jesperbrannmark
I will probably be beaten in public on this forum for this.
The only place I would save random datafiles created by my software would be "my documents" folder. When Microsoft released Vista and all my datafiles was moved to "virtualstore" I was a bit fed up. My Documents folder is not likely to change rights, but for all other you never know what their next move is.

For Mac.... I have asked myself the same question (including how can I find my documents folder?) but have no answer. Someone might be able to fill me in here?

Re: Where to save user data, per OS?

Posted: Fri Aug 12, 2011 7:23 pm
by kenmo
ts-soft, I know about GetHomeDirectory(), I mentioned it in my post, but I'm looking for specifics...

For example, on my Win XP, various software has saved to
[home]\Application Data\[name]\
[home]\Local Settings\Application Data\[name]\
[home]\Local Settings\Apps\[name]\

On Windows 7 right now, I have
[home]\AppData\Local\[name]\
[home]\AppData\LocalLow\[name]\ (??)
[home]\AppData\Roaming\[name]\ (??)

I don't have a Mac, but I believe the recommended path is also like
[home]/AppData/[name]/

Linux I have no idea what the standard software paths are....

Maybe I should worry about this later when I am in testing stages?

Re: Where to save user data, per OS?

Posted: Fri Aug 12, 2011 7:32 pm
by ts-soft
for linux is it the right path, but you have add your applicationdir with a dot at the beginning, to hide the dir.

I use something like this on all os:

Code: Select all

Procedure.s GetPrefFile()
  Protected PrefFile.s = GetHomeDirectory()

  PrefFile = ReplaceString(PrefFile, "\", "/")
  PrefFile + ".ts-soft"
  If FileSize(PrefFile) <> - 2
    CreateDirectory(PrefFile)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      SetFileAttributes(PrefFile, #PB_FileSystem_Hidden)
    CompilerEndIf
  EndIf
  PrefFile + "/ProgramName"
  If FileSize(PrefFile) <> - 2
    CreateDirectory(PrefFile)
  EndIf
  PrefFile + "/ProgramName.prefs"

  ProcedureReturn PrefFile
EndProcedure
and put all required stuff in it.

Re: Where to save user data, per OS?

Posted: Fri Aug 12, 2011 9:25 pm
by kenmo
Hmm, good suggestion, I did not know that a "." prefix makes a file hidden on Linux. Good tip. Is it only hidden from GUI interfaces, or even from "ls" terminal calls? (There's probably a "ls" flag to include/exclude hidden files.)

On Windows, I'm not too worried about hiding my data, so long as it's neatly tucked away in its own AppData\subdir.

Re: Where to save user data, per OS?

Posted: Fri Aug 12, 2011 9:36 pm
by luis
kenmo wrote:Hmm, good suggestion, I did not know that a "." prefix makes a file hidden on Linux. Good tip. Is it only hidden from GUI interfaces, or even from "ls" terminal calls? (There's probably a "ls" flag to include/exclude hidden files.)
It's similar to the +H attribute on windows, you can view a hidden file specifying the right switch for the console command.

Re: Where to save user data, per OS?

Posted: Sat Aug 13, 2011 1:59 am
by J. Baker
Mac OS X

/Users/name/Library/Application Support/yourfolder

Change "name" and "yourfolder". ;)

Re: Where to save user data, per OS?

Posted: Sat Aug 13, 2011 8:48 am
by jesperbrannmark
/Users/name/Library/Application Support/yourfolder
Change name to what?

Re: Where to save user data, per OS?

Posted: Sat Aug 13, 2011 11:30 am
by luis
jesperbrannmark wrote:Change name to what?
I'll take a wild, bold guess: "name" under the users folder... hmmm..... HMMMMM.... maybe the user's name ? Too bold ?

Re: Where to save user data, per OS?

Posted: Sat Aug 13, 2011 6:54 pm
by J. Baker
luis wrote:
jesperbrannmark wrote:Change name to what?
I'll take a wild, bold guess: "name" under the users folder... hmmm..... HMMMMM.... maybe the user's name ? Too bold ?
Bingo! It's the users name. I'm sure there's some code to get the users name but haven't tried myself yet.

Re: Where to save user data, per OS?

Posted: Sat Aug 13, 2011 8:52 pm
by kenmo
Thanks for that J.

What does GetHomeDirectory() return on your (or any) Mac? Probably the "/Users/[name]/" that you mentioned?

So it's probably smart to create and write to GetHomeDirectory() + "Library/Application Support/[appname]/"?

Maybe if everyone agrees on these 'standard' locations, they could be added to the FAQ thread.

Re: Where to save user data, per OS?

Posted: Sat Aug 13, 2011 9:06 pm
by WilliamL
That makes sense to me but I would add that the files in the Application Support folder should be in a folder of the App name.

GetHomeDirectory()+ "Library/Application Support/"+[app name folder]/file

Of course, preferences go in the GetHomeDirectory()+ "Library/Preferences/" folder and resources stay with the app in the App/Contents/[Resources/] folder.

Re: Where to save user data, per OS?

Posted: Sun Aug 14, 2011 4:07 am
by J. Baker
kenmo wrote:Thanks for that J.

What does GetHomeDirectory() return on your (or any) Mac? Probably the "/Users/[name]/" that you mentioned?

So it's probably smart to create and write to GetHomeDirectory() + "Library/Application Support/[appname]/"?

Maybe if everyone agrees on these 'standard' locations, they could be added to the FAQ thread.
It does return "/Users/YourName/" like you stated. ;)

Re: Where to save user data, per OS?

Posted: Sun Aug 14, 2011 8:46 am
by buddymatkona
This might be a good place to mention all PB tools should be changed to put files in the USER area rather than the Program Files area. Pbcompiler.exe may still try to write PureBasic.asm in the compiler directory. You can run into trouble trying to see ASM output from a Windows 7 command window with all default settings.