首页 游戏 软件 排行 智能

在delphi 获取特殊目录 SHGetSpecialFolderLocation SHGetPathFromIDList

来源: 本站整理 日期:2015/7/21 10:31:21

在写应用程序的时候,我们经常会用到读取windows系统的一些特殊的目录。比如 桌面 快速启动栏的路径等等。

下面介绍两个API函数 SHGetSpecialFolderLocation SHGetPathFromIDList

我们先看代码:

通过 SHGetSpecialFolderLocation、SHGetPathFromIDList 函数获取常用路径

这个是在windows下显示的结果


这是在 win7下面显示的结果

0:C:\Users\Administrator\Desktop

1:

2:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

3:

4:

5:C:\Users\Administrator\Documents

6:C:\Users\Administrator\Favorites

7:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

8:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Recent

9:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\SendTo

10:

11:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu

12:

13:C:\Users\Administrator\Music

14:C:\Users\Administrator\Videos

15:

16:C:\Users\Administrator\Desktop

17:

18:

19:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Network Shortcuts

20:C:\Windows\Fonts

21:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Templates

22:C:\ProgramData\Microsoft\Windows\Start Menu

23:C:\ProgramData\Microsoft\Windows\Start Menu\Programs

24:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

25:C:\Users\Public\Desktop

26:C:\Users\Administrator\AppData\Roaming

27:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Printer Shortcuts

28:C:\Users\Administrator\AppData\Local

29:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

30:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

31:C:\Users\Administrator\Favorites

32:C:\Users\Administrator\AppData\Local\Microsoft\Windows\Temporary Internet Files

33:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies

34:C:\Users\Administrator\AppData\Local\Microsoft\Windows\History

35:C:\ProgramData

36:C:\Windows

37:C:\Windows\System32

38:C:\Program Files (x86)

39:C:\Users\Administrator\Pictures

40:C:\Users\Administrator

41:C:\Windows\SysWOW64

42:C:\Program Files (x86)

43:C:\Program Files (x86)\Common Files

44:C:\Program Files (x86)\Common Files

45:C:\ProgramData\Microsoft\Windows\Templates

46:C:\Users\Public\Documents

47:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

48:C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools

49:

50:

51:

52:

53:C:\Users\Public\Music

54:C:\Users\Public\Pictures

55:C:\Users\Public\Videos

56:C:\Windows\Resources

57:

58:

59:C:\Users\Administrator\AppData\Local\Microsoft\Windows\Burn\Burn


文字结果 可以用 Ctrl+F 来查找


代码文件:unit Unit1;

interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;
type
 TForm1 = class(TForm)
  Memo1: TMemo;
  procedure FormCreate(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses ShlObj; {SHGetSpecialFolderLocation、SHGetPathFromIDList 函数来自这个单元}
{获取常用路径(的)函数}
function GetPath(FID: Integer): string;
var
 pidl: PItemIDList;
 path: array[0..MAX_PATH] of Char;
begin
 SHGetSpecialFolderLocation(0, FID, pidl);
 SHGetPathFromIDList(pidl, path);
 Result := path;
end;
{测试时, 我把 SHGetSpecialFolderLocation 函数需要(的)参数定义成了一个常数数组}
const
 arr: array[0..57] of Integer = (
  CSIDL_DESKTOP         , { = $0000 }
  CSIDL_INTERNET        , { = $0001 }
  CSIDL_PROGRAMS        , { = $0002 }
  CSIDL_CONTROLS        , { = $0003 }
  CSIDL_PRINTERS        , { = $0004 }
  CSIDL_PERSONAL        , { = $0005 }
  CSIDL_FAVORITES        , { = $0006 }
  CSIDL_STARTUP         , { = $0007 }
  CSIDL_RECENT         , { = $0008 }
  CSIDL_SENDTO         , { = $0009 }
  CSIDL_BITBUCKET        , { = $000a }
  CSIDL_STARTMENU        , { = $000b }
  CSIDL_MYDOCUMENTS       , { = $000c }
  CSIDL_MYMUSIC         , { = $000d }
  CSIDL_MYVIDEO         , { = $000e }
  CSIDL_DESKTOPDIRECTORY    , { = $0010 }
  CSIDL_DRIVES         , { = $0011 }
  CSIDL_NETWORK         , { = $0012 }
  CSIDL_NETHOOD         , { = $0013 }
  CSIDL_FONTS          , { = $0014 }
  CSIDL_TEMPLATES        , { = $0015 }
  CSIDL_COMMON_STARTMENU    , { = $0016 }
  CSIDL_COMMON_PROGRAMS     , { = $0017 }
  CSIDL_COMMON_STARTUP     , { = $0018 }
  CSIDL_COMMON_DESKTOPDIRECTORY , { = $0019 }
  CSIDL_APPDATA         , { = $001a }
  CSIDL_PRINTHOOD        , { = $001b }
  CSIDL_LOCAL_APPDATA      , { = $001c }
  CSIDL_ALTSTARTUP       , { = $001d }
  CSIDL_COMMON_ALTSTARTUP    , { = $001e }
  CSIDL_COMMON_FAVORITES    , { = $001f }
  CSIDL_INTERNET_CACHE     , { = $0020 }
  CSIDL_COOKIES         , { = $0021 }
  CSIDL_HISTORY         , { = $0022 }
  CSIDL_COMMON_APPDATA     , { = $0023 }
  CSIDL_WINDOWS         , { = $0024 }
  CSIDL_SYSTEM         , { = $0025 }
  CSIDL_PROGRAM_FILES      , { = $0026 }
  CSIDL_MYPICTURES       , { = $0027 }
  CSIDL_PROFILE         , { = $0028 }
  CSIDL_SYSTEMX86        , { = $0029 }
  CSIDL_PROGRAM_FILESX86    , { = $002a }
  CSIDL_PROGRAM_FILES_COMMON  , { = $002b }
  CSIDL_PROGRAM_FILES_COMMONX86 , { = $002c }
  CSIDL_COMMON_TEMPLATES    , { = $002d }
  CSIDL_COMMON_DOCUMENTS    , { = $002e }
  CSIDL_COMMON_ADMINTOOLS    , { = $002f }
  CSIDL_ADMINTOOLS       , { = $0030 }
  CSIDL_CONNECTIONS       , { = $0031 }
  CSIDL_COMMON_MUSIC      , { = $0035 }
  CSIDL_COMMON_PICTURES     , { = $0036 }
  CSIDL_COMMON_VIDEO      , { = $0037 }
  CSIDL_RESOURCES        , { = $0038 }
  CSIDL_RESOURCES_LOCALIZED   , { = $0039 }
  CSIDL_COMMON_OEM_LINKS    , { = $003a }
  CSIDL_CDBURN_AREA       , { = $003b }
  CSIDL_COMPUTERSNEARME     , { = $003d }
  CSIDL_PROFILES         { = $003e }
 );
{测试}
procedure TForm1.FormCreate(Sender: TObject);
var
 i: Integer;
 s: string;
begin
 Memo1.Clear;
 for i := 0 to Length(Arr) - 1 do
 begin
  s := GetPath(Arr[i]);
  if s<>'' then Memo1.Lines.Add(Format('%d:'#9'%s', [Arr[i], s]));
 end;
end;
end.
窗体文件:object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClientHeight = 318
 ClientWidth = 465
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 OnCreate = FormCreate
 PixelsPerInch = 96
 TextHeight = 13
 object Memo1: TMemo
  Left = 0
  Top = 0
  Width = 465
  Height = 318
  Align = alClient
  Lines.Strings = (
   'Memo1')
  ScrollBars = ssBoth
  TabOrder = 0
  ExplicitWidth = 409
 end
end

 

看一个本站编写的函数

//获取系统特殊文件夹路径(SHGetSpecialFolderLocation)
procedure getFolder(aLocation: integer;var path:string);//path为输出参数
 var
  pIdl: PItemIDList;
  hPath: PChar;
begin
   if SUCCEEDED(SHGetSpecialFolderLocation(0, aLocation, pidl)) then
  begin
   hPath := StrAlloc(max_path) ;
   SHGetPathFromIDList(pIdl, hPath) ;
   path:=strpas(hpath);
   StrDispose(hPath) ;
  end;
 end;

调用的时候就只需要

var

path:string;

 getFolder(16,path); 这样就可以了



玩家留言 跟帖评论
查看更多评论
相关文章
猜你喜欢
同类下载