Special Folders Enum

Started by stanl, January 06, 2024, 05:15:04 AM

Previous topic - Next topic

stanl

I have a situation where a VPN access has been replaced by VDI. My code would place configurations and other lookups in a C:\temp folder which, of course, persists with VPN but vanishes with VDI.


Investigated if data could be persisted in one of the special folders. WB makes it easy to identify folder paths. for example
Code (WINBATCH) Select


WshShell = CreateObject("WScript.Shell")
sfolder   = WshShell.SpecialFolders("MyDocuments")
Message("MyDocuments Object",sfolder)
WshShell = 0
Exit



I decided to complicate things by going the CLR route. So I created a map for the special folders enum and coded CLR to at least return a CLR type.
Code (WINBATCH) Select


Special = $"AdminTools=48
ApplicationData=26
CDBurning=59
CommonAdminTools=47
CommonApplicationData=35
CommonDesktopDirectory=25
CommonDocuments=46
CommonMusic=53
CommonOemLinks=58
CommonPictures=54
CommonProgramFiles=43
CommonProgramFilesX86=44
CommonPrograms=23
CommonStartMenu=22
CommonStartup=24
CommonTemplates=45
CommonVideos=55
Cookies=33
Desktop=0
DesktopDirectory=16
Favorites=6
Fonts=20
History=34
InternetCache=32
LocalApplicationData=28
LocalizedResources=57
MyComputer=17
MyDocuments=5
MyMusic=13
MyPictures=39
MyVideos=14
NetworkShortcuts=19
Personal=5
PrinterShortcuts=27
ProgramFiles=38
ProgramFilesX86=42
Programs=2
Recent=8
Resources=56
SendTo=9
StartMenu=11
Startup=7
System=37
SystemX86=41
Templates=21
UserProfile=40
Windows=36$"
Special = StrReplace(Special, @cr, "") ; Remove editor dependence
sFolders = MapCreate(Special, "=", @lf)


key = sFolders["MyDocuments"]
ObjectClrOption("useany", "System")
oENV = ObjectClrNew("System.Environment")
sfolder = ObjectClrType("System.Environment.SpecialFolder", key) 
Message("MyDocuments Object",sfolder)
Exit



There is a GetFolderPath() method in the System.Environment class but I have been unable to get it to return the path string. I think my failures have something to do with the method being a static method, and in spite of some earlier threads where statics were discussed I am still confused. [and embarrassed to show what I tried]




stanl

And a minor note:  using the shell scripting COM object is limited to these special folders


    • AllUsersDesktop
    • AllUsersStartMenu
    • AllUsersPrograms
    • AllUsersStartup
    • Desktop
    • Favorites
    • Fonts
    • MyDocuments
    • NetHood
    • PrintHood
    • Programs
    • Recent
    • SendTo
    • StartMenu
    • Startup
    • Templates


    • [/size]So trying most of the enum keys for CLR will fail.

    stanl

    Final note on this: [where USER is actual user]... just to illustrate the difference; had to use PS to iterate CLR enum as still haven't figured out in WB CLR... >:(




    ;using  WScript.Shell
    AllUsersDesktop = C:\Users\Public\Desktop
    AllUsersStartMenu = C:\ProgramData\Microsoft\Windows\Start Menu
    AllUsersPrograms = C:\ProgramData\Microsoft\Windows\Start Menu\Programs
    AllUsersStartup = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
    Desktop = C:\Users\USER\OneDrive\Desktop
    Favorites = C:\Users\USER\Favorites
    Fonts = C:\Windows\Fonts
    MyDocuments = C:\Users\USER\OneDrive\Documents
    NetHood = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Network Shortcuts
    PrintHood = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
    Programs = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
    Recent = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Recent
    SendTo = C:\Users\USER\AppData\Roaming\Microsoft\Windows\SendTo
    StartMenu = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu
    Startup = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
    Templates = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Templates


    ;using CLR Enum
    AdminTools = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
    ApplicationData = C:\Users\USER\AppData\Roaming
    CDBurning = C:\Users\USER\AppData\Local\Microsoft\Windows\Burn\Burn
    CommonAdminTools = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
    CommonApplicationData = C:\ProgramData
    CommonDesktopDirectory = C:\Users\Public\Desktop
    CommonDocuments = C:\Users\Public\Documents
    CommonMusic = C:\Users\Public\Music
    CommonOemLinks =
    CommonPictures = C:\Users\Public\Pictures
    CommonProgramFiles = C:\Program Files\Common Files
    CommonProgramFilesX86 = C:\Program Files (x86)\Common Files
    CommonPrograms = C:\ProgramData\Microsoft\Windows\Start Menu\Programs
    CommonStartMenu = C:\ProgramData\Microsoft\Windows\Start Menu
    CommonStartup = C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
    CommonTemplates = C:\ProgramData\Microsoft\Windows\Templates
    CommonVideos = C:\Users\Public\Videos
    Cookies = C:\Users\USER\AppData\Local\Microsoft\Windows\INetCookies
    Desktop = C:\Users\USER\OneDrive\Desktop
    DesktopDirectory = C:\Users\USER\OneDrive\Desktop
    Favorites = C:\Users\USER\Favorites
    Fonts = C:\WINDOWS\Fonts
    History = C:\Users\USER\AppData\Local\Microsoft\Windows\History
    InternetCache = C:\Users\USER\AppData\Local\Microsoft\Windows\INetCache
    LocalApplicationData = C:\Users\USER\AppData\Local
    LocalizedResources =
    MyComputer =
    MyDocuments = C:\Users\USER\OneDrive\Documents
    MyMusic = C:\Users\USER\Music
    MyPictures = C:\Users\USER\OneDrive\Pictures
    MyVideos = C:\Users\USER\Videos
    NetworkShortcuts = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Network Shortcuts
    Personal = C:\Users\USER\OneDrive\Documents
    PrinterShortcuts = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
    ProgramFiles = C:\Program Files
    ProgramFilesX86 = C:\Program Files (x86)
    Programs = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
    Recent = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Recent
    Resources = C:\WINDOWS\resources
    SendTo = C:\Users\USER\AppData\Roaming\Microsoft\Windows\SendTo
    StartMenu = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu
    Startup = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
    System = C:\WINDOWS\system32
    SystemX86 = C:\WINDOWS\SysWOW64
    Templates = C:\Users\USER\AppData\Roaming\Microsoft\Windows\Templates
    UserProfile = C:\Users\USER
    Windows = C:\WINDOWS

    td

    Unlike almost all Framework enumerations the "System.Environment.SpecialFolder" enumeration does not provide a CLR "Type" object when requested.  WIL CLR hosting needs that type object to find the method being called. I have no idea why that is the case. Until or unless other information ever appears, add it to the list of inconsistencies in MSFT's .Net implementation.
    "No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
      - Dr. Tom Cade

    td

    This doesn't list all the available folders but does provide access to all special folders including those not listed as environment variables.

    Code (winbatch) Select
    Shell = CreateObject("Shell.Application")
    ; You can also use the Apps special folder`s GUID:
    ;   shell:::{4234d49b-0245-4df3-b780-3893943456e1}
    AppFolder = Shell.NameSpace("shell:Appsfolder")
    Apps = AppFolder.Items()
    Names = ""
    foreach App in Apps
       Names := App.Name:@lf
    next
    ; Check for the Notepad "Windows Store" applications.
    if ItemLocate("Notepad",Names, @lf) then HasApp = "True"
    else HasApp = "False"

    Message("Appliction Folder has Notepad", HasApp)
    exit
    "No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
      - Dr. Tom Cade

    stanl

    After all, for the intent, special folder code not needed. Client source will permit folder access that can be mapped/unmapped  rather than using SFTP.