我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用win32com.shell.shell.SHGetSpecialFolderPath()。
def testShellLink(self): desktop = str(shell.SHGetSpecialFolderPath(0, CSIDL_DESKTOP)) num = 0 shellLink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) persistFile = shellLink.QueryInterface(pythoncom.IID_IPersistFile) names = [os.path.join(desktop, n) for n in os.listdir(desktop)] programs = str(shell.SHGetSpecialFolderPath(0, CSIDL_PROGRAMS)) names.extend([os.path.join(programs, n) for n in os.listdir(programs)]) for name in names: try: persistFile.Load(name,STGM_READ) except pythoncom.com_error: continue # Resolve is slow - avoid it for our tests. #shellLink.Resolve(0, shell.SLR_ANY_MATCH | shell.SLR_NO_UI) fname, findData = shellLink.GetPath(0) unc = shellLink.GetPath(shell.SLGP_UNCPRIORITY)[0] num += 1 if num == 0: # This isn't a fatal error, but is unlikely. print "Could not find any links on your desktop or programs dir, which is unusual"
def testShellLink(self): desktop = str(shell.SHGetSpecialFolderPath(0, CSIDL_DESKTOP)) num = 0 shellLink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) persistFile = shellLink.QueryInterface(pythoncom.IID_IPersistFile) names = [os.path.join(desktop, n) for n in os.listdir(desktop)] programs = str(shell.SHGetSpecialFolderPath(0, CSIDL_PROGRAMS)) names.extend([os.path.join(programs, n) for n in os.listdir(programs)]) for name in names: try: persistFile.Load(name,STGM_READ) except pythoncom.com_error: continue # Resolve is slow - avoid it for our tests. #shellLink.Resolve(0, shell.SLR_ANY_MATCH | shell.SLR_NO_UI) fname, findData = shellLink.GetPath(0) unc = shellLink.GetPath(shell.SLGP_UNCPRIORITY)[0] num += 1 if num == 0: # This isn't a fatal error, but is unlikely. print("Could not find any links on your desktop or programs dir, which is unusual")
def DumpFavorites(): favfold = str(shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_FAVORITES)) print "Your favourites are at", favfold os.path.walk(favfold, FavDumper, None)
def get_special_folder_path(path_name): import pythoncom from win32com.shell import shell, shellcon for maybe in """ CSIDL_COMMON_STARTMENU CSIDL_STARTMENU CSIDL_COMMON_APPDATA CSIDL_LOCAL_APPDATA CSIDL_APPDATA CSIDL_COMMON_DESKTOPDIRECTORY CSIDL_DESKTOPDIRECTORY CSIDL_COMMON_STARTUP CSIDL_STARTUP CSIDL_COMMON_PROGRAMS CSIDL_PROGRAMS CSIDL_PROGRAM_FILES_COMMON CSIDL_PROGRAM_FILES CSIDL_FONTS""".split(): if maybe == path_name: csidl = getattr(shellcon, maybe) return shell.SHGetSpecialFolderPath(0, csidl, False) raise ValueError("%s is an unknown path ID" % (path_name,))
def get_system_dir(): import win32api # we assume this exists. try: import pythoncom import win32process from win32com.shell import shell, shellcon try: if win32process.IsWow64Process(): return shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_SYSTEMX86) return shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_SYSTEM) except (pythoncom.com_error, win32process.error): return win32api.GetSystemDirectory() except ImportError: return win32api.GetSystemDirectory()
def DumpFavorites(): favfold = str(shell.SHGetSpecialFolderPath(0, shellcon.CSIDL_FAVORITES)) print("Your favourites are at", favfold) os.path.walk(favfold, FavDumper, None)