如何获取开始菜单程序目录的路径?

时间:2010-02-07 06:26:03

标签: python windows

...对于当前用户?对所有用户?

我正在开发一个需要在开始菜单中创建链接的小程序。目前我正在硬编码如下,但它只适用于英语语言环境,例如它应该是德语中的“Startmenü”。什么是更清洁,更便携的方法?

OUR_STARTMENU = os.environ['ALLUSERSPROFILE'] + '\Start Menu\Programs\Our Stuff'

谢谢

3 个答案:

答案 0 :(得分:10)

我听说有两种方法可以做到这一点。第一:

from win32com.shell import shell
shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_COMMON_STARTMENU)

其次,使用WScript.Shell对象(来源:http://www.mail-archive.com/python-win32@python.org/msg00992.html):

import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell")
allUserProgramsMenu = objShell.SpecialFolders("AllUsersPrograms")
userMenu = objShell.SpecialFolders("StartMenu")

另一个来源:http://blogs.msdn.com/saveenr/archive/2005/12/28/creating-a-start-menu-shortcut-with-powershell-and-python.aspx

答案 1 :(得分:2)

此外,CSIDL_COMMON_STARTMENU适用于所有用户启动,CSIDL_STARTMENU适用于当前用户启动。

答案 2 :(得分:1)

Environment.gov.au的朋友Luke Pinner通过电子邮件提供了一个使用核心模块(python 2.5+)的解决方案。由于API调用的返回是unicode,因此被认为是多语言的。通过日语语言环境在Win7上进行测试,并通过手动更改“开始”菜单指向%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