有没有哪个函数能获取到Documents and Settings/All Users/Application Data文件夹位置?(100分)

  • 主题发起人 主题发起人 wyjpg
  • 开始时间 开始时间
这个文件夹属于系统盘里的,因此只要获取当前系统盘即可。
 
uses ShlObj;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; TempPath: array [0..255] of char;<br>begin<br>&nbsp; &nbsp; if ShGetSpecialFolderPath(Application.Handle, TempPath, 35, False) then<br>&nbsp; &nbsp; showmessage(TempPath);<br>end;
 
取得桌面等特殊文件夹的路径<br>http://203.208.37.104/search?q=c ... zx9YCnssyVcYK0dpDfA<br><br>要引用 &nbsp; shlobj &nbsp; : &nbsp; <br>&nbsp; function &nbsp; GetDesktopFolder: &nbsp; string; &nbsp; <br>&nbsp; var &nbsp; <br>&nbsp; &nbsp; &nbsp; Buffer: &nbsp; PChar; &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; Result &nbsp; := &nbsp; ''; &nbsp; <br>&nbsp; &nbsp; &nbsp; GetMem(Buffer, &nbsp; MAX_PATH); &nbsp; <br>&nbsp; &nbsp; &nbsp; try &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; ShGetSpecialFolderPath(Application.Handle,Buffer, &nbsp; CSIDL_DESKTOP, &nbsp; False) &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetString(Result, &nbsp; Buffer, &nbsp; StrLen(Buffer)); &nbsp; <br>&nbsp; &nbsp; &nbsp; finally &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeMem(Buffer); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; end; &nbsp; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; 或者 &nbsp; <br>&nbsp; 也要引用shlobj &nbsp; &nbsp; <br>&nbsp; function &nbsp; GetDesktopDir: &nbsp; string; &nbsp; <br>&nbsp; var &nbsp; <br>&nbsp; &nbsp; &nbsp; Buffer: &nbsp; PChar; &nbsp; <br>&nbsp; &nbsp; &nbsp; ItemIDList: &nbsp; PItemIDList; &nbsp; <br>&nbsp; &nbsp; &nbsp; ShellMalloc: &nbsp; IMalloc; &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; Result &nbsp; := &nbsp; ''; &nbsp; <br>&nbsp; &nbsp; &nbsp; if &nbsp; (ShGetMalloc(ShellMalloc) &nbsp; = &nbsp; S_OK) &nbsp; and &nbsp; (ShellMalloc &nbsp; &lt;&gt; &nbsp; nil) &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Buffer &nbsp; := &nbsp; ShellMalloc.Alloc(MAX_PATH); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; SHGetSpecialFolderLocation(Application.Handle, &nbsp; CSIDL_DESKTOP, &nbsp; ItemIDList) &nbsp; = &nbsp; S_OK &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; SHGetPathFromIDList(ItemIDList,Buffer) &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetString(Result, &nbsp; Buffer, &nbsp; StrLen(Buffer)); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShellMalloc.Free(Buffer); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; <br>Top<br><br>3 楼xikug(xIkUg)回复于 2003-01-26 14:06:00 得分 70利用Api函数,利用他们就可以轻松简单的获取这些特殊系统目录。 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function &nbsp; SHGetSpecialFolderLocation(hwndOwner: &nbsp; HWND; &nbsp; nFolder: &nbsp; Integer; &nbsp; <br>&nbsp; var &nbsp; ppidl: &nbsp; PItemIDList): &nbsp; HResult; &nbsp; stdcall; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function &nbsp; SHGetPathFromIDList(pidl: &nbsp; PItemIDList; &nbsp; pszPath: &nbsp; PChar): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BOOL;stdcall; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 其中由nFolder参数指定的就是各个特殊系统目录: &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_DESKTOP:毫无疑问这就是桌面; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_DRIVERS:我的电脑; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_FAVORITES:收藏夹; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_STARTUP:开始菜单; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_NETWORK:网上邻居; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 还有很多,你可以查阅一下Delphi的Win32 &nbsp; Api函数的帮助文件,不过在帮助文件下的这些参数也不是很全,像收藏夹帮助文件里面就没有,你可以查阅一下它的头文件:shlobj.pas。 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 下面我就利用这两个函数取得桌面的路径(在win98和win2000下都可以使用): &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uses &nbsp; shlobj; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pitemITEMIDLIST; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:string; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shGetSpecialFolderLocation(handle,CSIDL_DESKTOP,pitem); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setlength(s,100); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shGetPathFromIDList(pitem,pchar(s)); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 在字符串s中存储的就是桌面的路径值。简单吧!<br>============== &nbsp; &nbsp;<br>&nbsp; 如何取得某些特殊文件夹路径 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; 在Windows &nbsp; 下有很多的特殊含义的文件夹,如桌面,网上邻居,字体,我的文档, &nbsp; <br>&nbsp; 程序组,最近文档,发送到,开始菜单,启动等,有时可能需要取得这些文件夹 &nbsp; <br>&nbsp; 的真正路径。 &nbsp; <br>&nbsp; 一种方法是通过读注册表文件,存放在HKEY_CURRENT_USER根下,目录为: &nbsp; <br>&nbsp; Software/MicroSoft/Windows/CurrentVersion/Explorer &nbsp; <br>&nbsp; 可看到在shell &nbsp; folder段中存放着上述文件夹所对应的实际物理目录,至于如何 &nbsp; <br>&nbsp; 读注册表的操作不再给出,请自行解决。 &nbsp; <br><br>&nbsp; 另一种方法是调用shell函数可以得到相应的目录,但是不是所有在shell &nbsp; folder &nbsp; <br>&nbsp; 目录下的文件夹都可以获得,请注意。 &nbsp; <br>&nbsp; function &nbsp; GetSpecialFolderDir(const &nbsp; folderid:integer):string; &nbsp; <br>&nbsp; var &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pidl:pItemIDList; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer:array &nbsp; [ &nbsp; 0..255 &nbsp; ] &nbsp; of &nbsp; char &nbsp; ; &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //取指定的文件夹项目表 &nbsp; <br>&nbsp; &nbsp; &nbsp; SHGetSpecialFolderLocation( &nbsp; application.Handle &nbsp; , &nbsp; folderid, &nbsp; pidl); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SHGetPathFromIDList(pidl, &nbsp; buffer); &nbsp; &nbsp; &nbsp; &nbsp; //转换成文件系统的路径 &nbsp; <br>&nbsp; &nbsp; &nbsp; result:=strpas(buffer); &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; 其中:folderid可以取下面的值:但是请注意,有些是虚的文件夹,不是文件系统 &nbsp; <br>&nbsp; 的一部分,所以用SHGetPathFromIDList是取不出路径的,但是在此也列出了。打'*' &nbsp; <br>&nbsp; 号的为不是真正的文件系统,应该用作它用。 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_BITBUCKET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; 回收站 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_CONTROLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; 控制面板 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_DESKTOP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; 桌面 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_DESKTOPDIRECTORY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 桌面目录 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/Desktop &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_DRIVES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; 我的电脑 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_FONTS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 字体 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/FONTS &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_NETHOOD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 网上邻居目录 &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/NetHood &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_NETWORK &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; 网上邻居 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_PERSONAL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 我的文档 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/My &nbsp; Documents &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_PRINTERS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * &nbsp; &nbsp; &nbsp; 打印机 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_PROGRAMS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 程序组 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/Start &nbsp; Menu/Programs &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_RECENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 最近文档 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/Recent &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_SENDTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 发送到 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/SentTo &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_STARTMENU &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 开始菜单 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/Start &nbsp; Menu &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_STARTUP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 启动 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/启动 &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CSIDL_TEMPLATES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 模版 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如C:/WINDOWS/ShellNew
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部