DELPHI 获取特殊系统目录
利用Api函数,现在我介绍两个Api函数,利用他们就可以轻松简单的获取这些特殊系统目录。
Function SHGetSpecialFolderLocation(hwndOwner: HWND; nFolder: Integer;
var ppidl: PItemIDList): HResult; stdcall;
Function SHGetPathFromIDList(pidl: PItemIDList; pszPath: PChar): BOOL;stdcall;
其中由nFolder参数指定的就是各个特殊系统目录:
CSIDL_DESKTOP:毫无疑问这就是桌面;
CSIDL_DRIVERS:我的电脑;
CSIDL_FAVORITES:收藏夹;
CSIDL_STARTUP:开始菜单;
CSIDL_NETWORK:网上邻居;
uses shlobj;
var
pitem:PITEMIDLIST;
s:string;
begin
shGetSpecialFolderLocation(handle,CSIDL_DESKTOP,pitem);
setlength(s,100);
shGetPathFromIDList(pitem,pchar(s));
end;
vName:= copy(s,1, Length(s)-pos('桌面',s)-15)+'\'; //////减15是为了减去 后面的 “#0#0.......”
---------------------------------------------------------------------------------------------------------------------------------
方法一:通过访问注册表
uses registry;
procedure Tsysdirfrm.FormShow(Sender: TObject);
var reg:Tregistry;
begin
reg:=Tregistry.create; reg.RootKey:=HKEY_USERS;
reg.OpenKey('\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',false);
listbox1.items.add(reg.readstring('Desktop')) ;//桌面目录
listbox1.items.add(reg.readstring('Fonts'));//字体目录
listbox1.items.add(reg.readstring('NetHood')) ;//网上邻居
listbox1.items.add(reg.readstring('Personal'));//我的文档目录
listbox1.items.add(reg.readstring('Programs'));//开始菜单程序目录
listbox1.items.add(reg.readstring('Recent')) ;//存放用户最近访问文件快捷方式的目录
listbox1.items.add(reg.readstring('SendTo'));//发送到目录
listbox1.items.add(reg.readstring('Start Menu'));//开始菜单目录
listbox1.items.add(reg.readstring('Startup'));//开始菜单启动项目录
listbox1.items.add(reg.readstring('Favorites')) ;//收藏夹目录
listbox1.items.add(reg.readstring('History'));//网页历史目录
listbox1.items.add(reg.readstring('Cookies'));//cookies目录
listbox1.items.add(reg.readstring('Cache')) ;//缓存目录
listbox1.items.add(reg.readstring('AppData'));//应用程序数据目录
listbox1.items.add(reg.readstring('PrintHood')) ;//打印目录
reg.CloseKey ;
reg.free;
end;
方法二:使用API函数SHGetSpecialFolderLocation和SHGetPathFromIDList
uses shlobj;
procedure TForm1.Button1Click(Sender: TObject);
var pidl: PItemIDList;
FavPath: array[0..MAX_PATH] of char;
temp:array [1..17] of integer; i:byte;
begin
temp[1]:=CSIDL_DESKTOP; //桌面
temp[2]:=CSIDL_DESKTOPDIRECTORY ; //桌面目录
temp[3]:=CSIDL_FONTS ; //字体目录
temp[4]:=CSIDL_NETHOOD ; //网络邻居
temp[5]:=CSIDL_PERSONAL ; //我的文档目录
temp[6]:=CSIDL_PROGRAMS ; //开始菜单程序目录
temp[7]:=CSIDL_RECENT ; //存放用户最近访问文件快捷方式的目录
temp[8]:=CSIDL_SENDTO ; //"发送到"目录
temp[9]:=CSIDL_STARTMENU ; //开始菜单目录
temp[10]:=CSIDL_STARTUP ; //开始菜单启动项目录
temp[11]:=CSIDL_FAVORITES ; //收藏夹目录
for i:=1 to 17 do
begin
SHGetSpecialFolderLocation(Handle, temp[i], pidl);
SHGetPathFromIDList(pidl, favpath);
listbox1.Items.Add(StrPas(FavPath));
end;
end;
-------------------------------------------------------------------------------------------------------------------
uses shlobj;
procedure Tgongzhi1.dsbkSpeedButton1Click(Sender: TObject);
var vPath, vName,s: string;
pitem:PITEMIDLIST;
begin
if DBGrid1.Enabled=False then
MessageBox(handle,'请先查询再进行导出操作!','友情提醒:',mb_ok+MB_ICONASTERISK)
else
begin
if Application.MessageBox('确定要导出EXCEL表? ','友情提醒:',MB_YESNO+MB_ICONQUESTION) = IDYES then
begin
shGetSpecialFolderLocation(handle,CSIDL_DESKTOP,pitem);
setlength(s,100);
shGetPathFromIDList(pitem,pchar(s));
vPath:= copy(s,1, Length(s)-pos('桌面',s)-15)+'\';
//vPath := 'C:\Documents and Settings\Administrator\桌面\';
vName :='工资汇总'+formatdatetime('yyyy-mm-dd',DateTimePicker1.date)+'~'+formatdatetime('yyyy-mm-dd',DateTimePicker2.date);
if DBGrid1.Enabled=False then
MessageBox(handle,'请先查询再进行删除操作!','友情提醒:',mb_ok+MB_ICONASTERISK)
else
savexls(ADOQuery1, VName, vPath);
MessageBox(handle,PChar('文件: '+vName+'; 路径:'+vPath+'; 导出成功!'),'友情提醒:',mb_ok+MB_ICONASTERISK)
end;
end;
end;