在delphi 获取特殊目录 SHGetSpecialFolderLocation SHGetPathFromIDList

前往Delphi专题

在写应用程序的时候,我们经常会用到读取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); 这样就可以了



网友评论
最新图文
  • 在delphi 获取特殊目录 SHGetSpecialFolderLocation SHGetPathFromIDList

    在写应用程序的时候,我们经常会用到读取windows系统的一些特殊的目录。比如 桌面 快速启动栏的路径等等。下面介绍两个API函数 SHGetSpecialFolderLocation SHGetPathFromIDList我们先看代码:  代码文件:unit Unit1;interfaceuses Wind,西西hvbet688园-最安全的下载资讯站。

  • Delphi通过ADO读写数据库

    ADO是一种程序对象,用于表示用户数据库中的数据结构和所包含的数据。ADO (ActiveX Data Objects,ActiveX数据对象)是Microsoft提出的应用程序接口(API)用以实现访问关系或非关系数据库中的数据。例如,如果您希,西西hvbet688园-最安全的下载资讯站。

  • Delphi如何实现抓图技术

    摘 要:本文以Delphi7.0作为开发平台,给出了网络监控hvbet688中的两种屏幕抓图技术的设计方法和步骤。介绍了教师在计算机机房内教学时,如何监控学生计算机显示器上的画面,以保证教学的质量和效果。  引言  随着网络技术的飞速发展,计算机网络在各高等院校教学中,西西hvbet688园-最安全的下载资讯站。

  • delphi中使用ADOQuery时的几个问题解决方案

    问题1、文参数的支持问题今天在使用ClientDataSource查询的时候,“divece_name”参数赋值为“%笔记本电脑%”,但是使用SQL Server Profiler跟踪后发现查询的语句变为:查询的数据提交到数据库时不完整,当我把N'@P1varchar(8)', '%笔记本%'就可,西西hvbet688园-最安全的下载资讯站。

  • android计时器、android 格式化时间计数器的实现

    完整的Android程序,主要功能是实现秒表计时需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34。点击停止的时候停止计时。问题:使用Calendar DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设,西西hvbet688园-最安全的下载资讯站。