Recent

Author Topic: [SOLVED] Get user's My Documents folder  (Read 6659 times)

JD

  • Hero Member
  • *****
  • Posts: 1796
[SOLVED] Get user's My Documents folder
« on: February 14, 2017, 11:21:29 am »
Hi there everyone,

I want to know how to get a user's My Documents folder so that I can save files there. I know how to do it in Delphi using TPath but I don't know how it can be done in Lazarus.

Cheers,

JD

« Last Edit: February 14, 2017, 12:16:58 pm by JD »
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

sky_khan

  • Guest
Re: Get user's My Documents folder
« Reply #1 on: February 14, 2017, 11:37:53 am »
Code: Pascal  [Select][+][-]
  1. uses windirs;
  2. ...
  3. Result:=GetWindowsSpecialDir(CSIDL_PERSONAL);
  4.  

JD

  • Hero Member
  • *****
  • Posts: 1796
Re: Get user's My Documents folder
« Reply #2 on: February 14, 2017, 11:48:17 am »
Code: Pascal  [Select][+][-]
  1. uses windirs;
  2. ...
  3. Result:=GetWindowsSpecialDir(CSIDL_PERSONAL);
  4.  

That was quick. It works. Thanks a million.

Do you know how I can do the same on Linux & maybe  on Mac too.

Thanks
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: Get user's My Documents folder
« Reply #3 on: February 14, 2017, 11:52:27 am »
Quote
@JD
I want to know how to get a user's My Documents folder so that I can save files there. I know how to do it in Delphi using TPath but I don't know how it can be done in Lazarus.
This will work cross platform way:
Code: Pascal  [Select][+][-]
  1. uses LazFileUtils;
  2. //...
  3. AppendPathDelim(GetUserDir + 'Documents')

JD

  • Hero Member
  • *****
  • Posts: 1796
Re: Get user's My Documents folder
« Reply #4 on: February 14, 2017, 12:05:23 pm »
Quote
@JD
I want to know how to get a user's My Documents folder so that I can save files there. I know how to do it in Delphi using TPath but I don't know how it can be done in Lazarus.
This will work cross platform way:
Code: Pascal  [Select][+][-]
  1. uses LazFileUtils;
  2. //...
  3. AppendPathDelim(GetUserDir + 'Documents')

Thanks a million GetMem. Works perfectly.  :D

I do have another question though. Will this work for all non-English OSes? It works for French (my locale) but what about others. I know the internal reference is in English but I just want to be sure.

JD
« Last Edit: February 14, 2017, 12:11:29 pm by JD »
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

sky_khan

  • Guest
Re: Get user's My Documents folder
« Reply #5 on: February 14, 2017, 12:21:19 pm »
I dont think GetUserDir + "Documents" is cross platform. Even on Windows it is not fixed. Old versions has "My Documents". It differs for each version and language.
Ubuntu and deriatives has "Documents" at Home=GetUserDir directory but other distros may not etc.

I think You will need some IFDEFS for cross platform

JD

  • Hero Member
  • *****
  • Posts: 1796
Re: Get user's My Documents folder
« Reply #6 on: February 14, 2017, 12:32:56 pm »
I dont think GetUserDir + "Documents" is cross platform. Even on Windows it is not fixed. Old versions has "My Documents". It differs for each version and language.
Ubuntu and deriatives has "Documents" at Home=GetUserDir directory but other distros may not etc.

I think You will need some IFDEFS for cross platform

That's why I asked. I use Ubuntu derivatives. Thanks for the info.
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: [SOLVED] Get user's My Documents folder
« Reply #7 on: February 14, 2017, 12:55:00 pm »
@SkyKhan
It is cross-platform, however won't cover all the possible scenarios. In fact I don't think a bulletproof, cross-platform function is even possible. This is why Lazarus doesn't implement it in the first place. For example one of my Linux distro(Kali) doesn't even have a document folder.

@JD
What exactly are you trying to achieve? Store a config file? For that you have: GetAppConfigDir, GetAppConfigFile(with some issues on osx).  GetUserDir is also cross platform. Perhaps if you describe the problem, we can come up with a better solution(or not) :)
« Last Edit: February 14, 2017, 01:14:07 pm by GetMem »

JD

  • Hero Member
  • *****
  • Posts: 1796
Re: [SOLVED] Get user's My Documents folder
« Reply #8 on: February 14, 2017, 01:21:39 pm »
@JD
What exactly are you trying to achieve? Store a config file? For that you have: GetAppConfigDir, GetAppConfigFile(with some issues on osx).  GetUserDir is also cross platform. Perhaps if you describe the problem, we can come up with a better solution(or not) :).

As a fallout from my last report creation question, I've created PDF reports and I want them saved to the user's My Documents folder by default. It is just an attempt to make the report creation more user-friendly.

JD
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: [SOLVED] Get user's My Documents folder
« Reply #9 on: February 14, 2017, 01:44:57 pm »
Quote
@JD
As a fallout from my last report creation question, I've created PDF reports and I want them saved to the user's My Documents folder by default. It is just an attempt to make the report creation more user-friendly.
I would make it configurable. Once a default directory is set, just save the PDFs directly there, otherwise launch a TSelectDirectoryDialog and let the user choose the appropriate folder.

JD

  • Hero Member
  • *****
  • Posts: 1796
Re: [SOLVED] Get user's My Documents folder
« Reply #10 on: February 14, 2017, 02:04:51 pm »
Quote
@JD
As a fallout from my last report creation question, I've created PDF reports and I want them saved to the user's My Documents folder by default. It is just an attempt to make the report creation more user-friendly.
I would make it configurable. Once a default directory is set, just save the PDFs directly there, otherwise launch a TSelectDirectoryDialog and let the user choose the appropriate folder.

I've actually done all that and it works fine. I just never tested it on a non French OS so I wondered if the same user directory code that you suggested will work without modifications on other Latin based OSes.
Windows (10, 7) - Lazarus 2.1/FPC 3.2, Delphi

Indy 10.6 series; mORMot; Zeos 7.3; SQLite, Firebird, PostgreSQL & MariaDB; VirtualTreeView 5.5.3 R1

 

TinyPortal © 2005-2018