How to get special folders paths?

AntidotEAntidotE Member Posts: 61
edited 2008-10-02 in NAV Tips & Tricks
For example, I need to know, what is direct path to user's "My Documents" folder (i.e. in XP ENU, usually it's %HOMEDRIVE%%HOMEPATH%\<My Documents>, in XP RUS ...\<Рабочий Стол>, and, surely, user can change it to any another path, i.e. my is "E:\AllDocs\")
How to get this?
It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.

Answers

  • kinekine Member Posts: 12,562
    You can find it through registry...
    see e.g. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    HomePath and HomeDrive is in the Key "HKEY_CURRENT_USER\Volatile Environment". The definition for, example, Start Menu is in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders folder

    If you need this also:

    (in my meaning, "My Documents" is here)
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders


    (and 'Shared Docs')
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

    To read the Registry you can use the "Windos Scripting Host" Automation (WSHShell). There exist a function like "RegRead"

    Regards
    Do you make it right, it works too!
  • ajhvdbajhvdb Member Posts: 672
    You don't need the registry, below is easier:
      CREATE(LautWshShell);
      LvMyDocuments := 'MyDocuments';
      LtPath := FORMAT(LautWshShell.SpecialFolders.Item(LvMyDocuments));
      CLEAR(LautWshShell);
    
  • AntidotEAntidotE Member Posts: 61
    Thanks a lot, it works! =D>
    and, some example to move this to Tips'n'Tricks:

    Function
    GetFolderPath(Which : Text[20]) : Text[250]
    CREATE(WSHShell);
    EXIT(FORMAT(WSHShell.RegRead('HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\'+Which)));
    CLEAR(WSHShell);
    
    Locals
    Name	DataType	Subtype	Length
    WSHShell	Automation	'Windows Script Host Object Model'.WshShell
    

    Examples/list of possible parameter strings:
    MESSAGE('User Application Data Path: %1',GetFolderPath('AppData'));
    MESSAGE('User Cache Path: %1',GetFolderPath('Cache'));
    MESSAGE('User CD Burning Path: %1',GetFolderPath('CD Burning'));
    MESSAGE('User Cookies Path: %1',GetFolderPath('Cookies'));
    MESSAGE('User Desktop Path: %1',GetFolderPath('Desktop'));
    MESSAGE('User Favorites Path: %1',GetFolderPath('Favorites'));
    MESSAGE('User Fonts Path: %1',GetFolderPath('Fonts'));
    MESSAGE('User History Path: %1',GetFolderPath('History'));
    MESSAGE('User Local Application Data Path: %1',GetFolderPath('Local AppData'));
    MESSAGE('User Local Settings Path: %1',GetFolderPath('Local Settings'));
    MESSAGE('User My Music Path: %1',GetFolderPath('My Music'));
    MESSAGE('User My Pictures Path: %1',GetFolderPath('My Pictures'));
    MESSAGE('User My Video Path: %1',GetFolderPath('My Video'));
    MESSAGE('User NetHood Path: %1',GetFolderPath('NetHood'));
    MESSAGE('User My Documents Path: %1',GetFolderPath('Personal'));
    MESSAGE('User PrintHood Path: %1',GetFolderPath('PrintHood'));
    MESSAGE('User Programs Path: %1',GetFolderPath('Programs'));
    MESSAGE('User Recent Path: %1',GetFolderPath('Recent'));
    MESSAGE('User SendTo Path: %1',GetFolderPath('SendTo'));
    MESSAGE('User Start Menu Path: %1',GetFolderPath('Start Menu'));
    MESSAGE('User Startup Path: %1',GetFolderPath('Startup'));
    MESSAGE('User Templates Path: %1',GetFolderPath('Templates'));
    
    It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.
  • AntidotEAntidotE Member Posts: 61
    I was in process of writing previous post, when your appeared...
    ajhvdb wrote:
    You don't need the registry, below is easier:
      CREATE(LautWshShell);
      LvMyDocuments := 'MyDocuments';
      LtPath := FORMAT(LautWshShell.SpecialFolders.Item(LvMyDocuments));
      CLEAR(LautWshShell);
    

    Can you show the list of all possible parameters?
    It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.
  • AntidotEAntidotE Member Posts: 61
    Ah, btw, this all concerns XP, what about Vista? Is there the same regpath? I haven't vista here and can't look at...
    It is hard to swim against self bloodstream... (c) Old, experienced kamikadze.
  • kinekine Member Posts: 12,562
    The path I wrote is from Vista, it means same path, but may be more keys... there is backward-compatibility...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,094
    [Topic moved from 'NAV/Navision' forum to 'NAV Tips & Tricks' forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • gycsiakgycsiak Member Posts: 19
    Just a quick update on this one after 11 years to make it work on RTC-based clients without Automation Security warning on RTC:

    To do this, have a variable defined as DotNet variable and RunOnClient=true parameter setup:

    Name DataType Subtype Length
    Environment DotNet System.Environment.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    And if you would like to access i.e. Local AppData folder for any file saving operation, get the path string without "\" at the end:


    Folder := Environment.GetEnvironmentVariable('localappdata') + '\';
  • DuikmeesterDuikmeester Member Posts: 304
    @gycsiak or using the same class:
    Environment.GetFolderPath(Environment.SpecialFolder.System))
    

    https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder
Sign In or Register to comment.