设为首页 加入收藏

TOP

VC++得到系统特殊文件夹路径(一)
2013-04-24 12:11:24 】 浏览:642
Tags:得到 系统 特殊 文件夹 路径

  对Windows程序来说,得到系统特殊文件夹路径是个非常实用的功能。比如要执行一些系统程序像cmd.exe、mspaint.exe、ping.exe时最好加上绝对路径(通常为C:\WINDOWS\system32),否则有可能会出现找不到指定文件的错误。还有要创建桌面快捷方式、启动菜单快捷方式等等也须要使用系统特殊文件夹路径。

  在批处理中,要获取系统特殊文件夹路径非常简单。如%SystemDrive%就能得到系统盘符。在VC++(www.cppentry.com)中,主要通过SHGetSpecialFolderPath函数来获取系统特殊文件夹路径。下面来看看这个函数的介绍:

  一.SHGetSpecialFolderPath

  函数功能:Retrieves the path of a special folder

  函数原型:

  BOOLSHGetSpecialFolderPath(

  HWNDhwndOwner,

  LPTSTRlpszPath,

  int nFolder,

  BOOLfCreate

  );

  参数说明:

  第一个参数hwndOwner

  A handle to the owner window that the client should specify if it displays a dialog box or message box.

  第二个参数lpszPath

  A pointer to a null-terminated string that receives the drive and path of the specified folder. This buffer must be at least MAX_PATH characters in size.

  第三个参数nFolder

  A CSIDL that identifies the folder of interest. If a virtual folder is specified, this function will fail.

  第四个参数fCreate

  Indicates whether the folder should be created if it does not already exist. If this value is nonzero, the folder will be created. If this value is zero, the folder will not be created.

  二.代码示例

  给出获取部分系统特殊文件夹路径的示例代码:

  [cpp]

  // VC++(www.cppentry.com)得到系统特殊文件夹路径

  //By MoreWindows

  #include <windows.h>

  #include <shlobj.h>

  #include <stdio.h>

  int main()

  {

  printf("    VC++(www.cppentry.com)得到系统特殊文件夹路径  \n");

  printf

  printf(" -- By MoreWindows

  const int MAXN = 9;

  int nArrCSIDL[] = {

  CSIDL_WINDOWS,

  CSIDL_SYSTEM,

  CSIDL_PROGRAM_FILES,

  CSIDL_DESKTOP,

  CSIDL_FAVORITES,

  CSIDL_FONTS,

  CSIDL_COOKIES,

  CSIDL_HISTORY,

  CSIDL_APPDATA,

  };

  char *pstrCSIDL[] = {

  "CSIDL_WINDOWS",

  "CSIDL_SYSTEM",

  "CSIDL_PROGRAM_FILES",

  "CSIDL_DESKTOP",

  "CSIDL_FAVORITES",

  "CSIDL_FONTS",

  "CSIDL_COOKIES",

  "CSIDL_HISTORY",

  "CSIDL_APPDATA",

  };

  int i;

  for (i = 0; i < MAXN; i++)

  {

  char szBuffer[MAX_PATH];

  SHGetSpecialFolderPath(NULL, szBuffer, nArrCSIDL[i], FALSE);

  printf("%s\n\t%s\n", pstrCSIDL[i], szBuffer);

  }

   

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC++实现获取网络时间 下一篇VC++ 编程实现开机自启动

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目