SHGetSpecialFolderLocation 的使用方法

本文介绍了一种使用C++在Windows环境下获取用户收藏夹路径的方法。通过调用SHGetSpecialFolderLocation和SHGetPathFromIDList函数,可以将收藏夹的位置转化为实际文件路径。

详情,请查看MSDN:

http://msdn.microsoft.com/en-us/library/bb762203%28VS.85%29.aspx

 

这里只提供一小段代码:

 

在Python中,可先获取桌面路径,再遍历该路径下的所有文件夹。以下为几种获取桌面路径并获取所有文件夹路径的方法: #### 方法一:使用内置的`winreg` ```python import _winreg import os def get_desktop(): key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') return _winreg.QueryValueEx(key, "Desktop")[0] desktop_path = get_desktop() folders = [os.path.join(desktop_path, f) for f in os.listdir(desktop_path) if os.path.isdir(os.path.join(desktop_path, f))] for folder in folders: print(folder) ``` #### 方法二:使用`win32`扩展 ```python import win32api import win32con import os def get_desktop(): key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', 0, win32con.KEY_READ) return win32api.RegQueryValueEx(key, 'Desktop')[0] desktop_path = get_desktop() folders = [os.path.join(desktop_path, f) for f in os.listdir(desktop_path) if os.path.isdir(os.path.join(desktop_path, f))] for folder in folders: print(folder) ``` #### 方法三:使用`win32com.shell` ```python from win32com.shell import shell, shellcon import os def GetDesktopPath(): ilist = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP) return shell.SHGetPathFromIDList(ilist) desktop_path = GetDesktopPath() folders = [os.path.join(desktop_path, f) for f in os.listdir(desktop_path) if os.path.isdir(os.path.join(desktop_path, f))] for folder in folders: print(folder) ``` #### 方法四:使用Python内置的`os`库的`path`模块 ```python import os def GetDesktopPath(): return os.path.join(os.path.expanduser("~"), 'Desktop') desktop_path = GetDesktopPath() folders = [os.path.join(desktop_path, f) for f in os.listdir(desktop_path) if os.path.isdir(os.path.join(desktop_path, f))] for folder in folders: print(folder) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值