How to get path of Start Menu's Programs directory?

前端 未结 3 435
故里飘歌
故里飘歌 2020-12-10 16:16

...for current user? for all users?

I\'m working an a small program which needs to create links in the start menu. Currently I\'m hardcoding like below, but it only

3条回答
  •  孤城傲影
    2020-12-10 16:50

    A friend, Luke Pinner of Environment.gov.au, gave a solution by email which uses a core module (python 2.5+). Believed to be multi-lingual as the return from the API call is unicode. Tested on Win7 with Japanese locale, and on another us-english machine by manually changing Start Menu to point to %USERPROFILE%\Startmenü

    ''' Get windows special folders without pythonwin
        Example:
                import specialfolders
                start_programs = specialfolders.get(specialfolders.PROGRAMS)
    
    Code is public domain, do with it what you will. 
    
    Luke Pinner - Environment.gov.au, 2010 February 10
    '''
    
    #Imports use _syntax to mask them from autocomplete IDE's
    import ctypes as _ctypes
    from ctypes.wintypes import HWND as _HWND, HANDLE as _HANDLE,DWORD as _DWORD,LPCWSTR as _LPCWSTR,MAX_PATH as _MAX_PATH, create_unicode_buffer as _cub
    _SHGetFolderPath = _ctypes.windll.shell32.SHGetFolderPathW
    
    #public special folder constants
    DESKTOP=                             0
    PROGRAMS=                            2
    MYDOCUMENTS=                         5
    FAVORITES=                           6
    STARTUP=                             7
    RECENT=                              8
    SENDTO=                              9
    STARTMENU=                          11
    MYMUSIC=                            13
    MYVIDEOS=                           14
    NETHOOD=                            19
    FONTS=                              20
    TEMPLATES=                          21
    ALLUSERSSTARTMENU=                  22
    ALLUSERSPROGRAMS=                   23
    ALLUSERSSTARTUP=                    24
    ALLUSERSDESKTOP=                    25
    APPLICATIONDATA=                    26
    PRINTHOOD=                          27
    LOCALSETTINGSAPPLICATIONDATA=       28
    ALLUSERSFAVORITES=                  31
    LOCALSETTINGSTEMPORARYINTERNETFILES=32
    COOKIES=                            33
    LOCALSETTINGSHISTORY=               34
    ALLUSERSAPPLICATIONDATA=            35
    
    def get(intFolder):
        _SHGetFolderPath.argtypes = [_HWND, _ctypes.c_int, _HANDLE, _DWORD, _LPCWSTR]
        auPathBuffer = _cub(_MAX_PATH)
        exit_code=_SHGetFolderPath(0, intFolder, 0, 0, auPathBuffer)
        return auPathBuffer.value
    

提交回复
热议问题