Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to get location of Desktop folder on Win32

by Anonymous Monk
on Apr 08, 2004 at 09:55 UTC ( [id://343554]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can a script get locations of various special folders like Desktop, Start Menu, Application data directory, etc on Win32? Thanks in advance for the answer!
  • Comment on How to get location of Desktop folder on Win32

Replies are listed 'Best First'.
Re: How to get location of Desktop folder on Win32
by EdwardG (Vicar) on Apr 08, 2004 at 10:36 UTC

    Quick answer:

    use Win32 qw(CSIDL_APPDATA); print Win32::GetFolderPath(CSIDL_APPDATA);

    This is a question the most Win32 programmers ask at some point, so it is worth posting some comprehensive info from MSDN

    Whenever you access any of the special folders in the following list, your application should use the Win32 APIs to dynamically obtain the proper language-specific folder names. The preferred way to do this is using the SHGetFolderPath API with the appropriate CSIDL constant. This function behaves consistently across Windows 95, Windows 98, Windows NT 4.0, and Windows 2000.

    This API is redistributable via the SHFOLDER.DLL. SHFOLDER.DLL is installed by the Windows Installer redistributable. Software vendors are encouraged to redistribute this component as much as possible to enable this support on Windows operating systems prior to Windows 2000. Windows 2000 includes this DLL as a protected system file and, as such, this DLL cannot be replaced on Windows 2000 or greater.

    To help ensure your application can run on Windows 9x, Windows NT 4 as well as Windows 2000, always link to the SHGetFolderPath implementation in SHFOLDER.DLL. Windows 2000 natively implements SHGetFolderPath in SHELL32.DLL, but other versions of Windows do not include SHGetFolderPath in SHELL32.DLL.

    Standard FolderCSIDL Constant Name
    Alternate Startup ([user], DBCS)CSIDL_ALTSTARTUP
    Alternate Startup folder (All Users profile, DBCS) CSIDL_COMMON_ALTSTARTUP
    Application Data ([user] profile)CSIDL_APPDATA
    Application Data (All Users Profile)CSIDL_COMMON_APPDATA
    Control Panel virtual folderCSIDL_CONTROLS
    Cookies folderCSIDL_COOKIES
    Desktop (namespace root) CSIDL_DESKTOP
    Desktop folder ([user] profile) CSIDL_DESKTOPDIRECTORY
    Desktop folder (All Users profile) CSIDL_COMMON_DESKTOPDIRECTORY
    Favorites folder ([user] profile) CSIDL_FAVORITES
    Favorites folder (All Users profile) CSIDL_COMMON_FAVORITES
    Fonts virtual folder CSIDL_FONTS
    History folder CSIDL_HISTORY
    Internet Cache folder CSIDL_INTERNET_CACHE
    Internet virtual folder CSIDL_INTERNET
    Local (non-roaming) data repository for apps CSIDL_LOCAL_APPDATA
    My Computer virtual folder CSIDL_DRIVES
    My Pictures folder CSIDL_MYPICTURES
    Network Neighborhood directory CSIDL_NETHOOD
    Network Neighborhood root CSIDL_NETWORK
    Personal folder ([user] profile) CSIDL_PERSONAL
    Printers virtual folder CSIDL_PRINTERS
    PrintHood folder ([user] profile) CSIDL_PRINTHOOD
    Program Files folder CSIDL_PROGRAM_FILES
    Program Files folder for x86 apps on Alpha systems CSIDL_PROGRAM_FILESX86
    Programs folder (under Start menu in [user] profile) CSIDL_PROGRAMS
    Programs folder (under Start menu in All Users profile) CSIDL_COMMON_PROGRAMS
    Recent folder ([user] profile) CSIDL_RECENT
    Recycle Bin folder CSIDL_BITBUCKET
    SendTo folder ([user] profile) CSIDL_SENDTO
    Start menu ([user] profile) CSIDL_STARTMENU
    Start menu (All Users profile) CSIDL_COMMON_STARTMENU
    Startup folder ([user] profile) CSIDL_STARTUP
    Startup folder (All Users profile) CSIDL_COMMON_STARTUP
    System folder CSIDL_SYSTEM
    System folder for x86 applications on Alpha systems CSIDL_SYSTEMx86
    Templates folder ([user] profile) CSIDL_TEMPLATES
    User’s profile folder CSIDL_PROFILE
    Windows directory or SYSROOT CSIDL_WINDOWS
      Thanks for pointing out the existance of GetFolderPath. But note that this function is only available since perl 5.8.1, so for a compatible solution one has to use Win32::API or Win32::Registry.
Re: How to get location of Desktop folder on Win32
by eserte (Deacon) on Apr 08, 2004 at 10:08 UTC
    Use the API function SHGetSpecialFolderLocation. For an example usage, take a look at the get_special_folder function in Win32Util.
Re: How to get location of Desktop folder on Win32
by jdporter (Paladin) on Apr 08, 2004 at 14:51 UTC
    It's incredibly easy using Win32::OLE. You can use the application object named WScript.Shell.
    use Win32::OLE; my $wsh = new Win32::OLE 'WScript.Shell'; my $desktop_path = $wsh->SpecialFolders('Desktop');
    You can inquire about lots of other special folders using this interface:
    • Favorites
    • Fonts
    • MyDocuments
    • NetHood
    • PrintHood
    • Programs
    • Recent
    • SendTo
    • StartMenu
    • Startup
    • Templates
    I don't know what versions of Windows have the WScript.Shell interface. The only Perl dependency is the Win32::OLE module.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: How to get location of Desktop folder on Win32
by AcidHawk (Vicar) on Apr 08, 2004 at 10:36 UTC

    That kinda depends on what you are looking for...

    If you are looking for directories to Desktop/Start Menu/Application Data, for the current user you could always look for the USERPROFILE environment setting with $ENV{'USERPROFILE'} then those 'special' directories are all under there.

    You can then just add for example \\Desktop to that envirenment variable.

    Below is a simple untested example:

    my $path = $ENV{'USERPROFILE'}; $path =~s/\\/\//g; my $desktop_path = "$path/Desktop";
    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      This is not a good solution. In non-english localizations the directory names are different (e.g. "My Documents" is "Meine Dokumente" in the German Windows version). Using an API function is the better solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://343554]
Approved by EdwardG
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found