当前位置:编程学习 > VC++ >>

VC++得到系统特殊文件夹路径

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

       在批处理中,要获取系统特殊文件夹路径非常简单。如%SystemDrive%就能得到系统盘符。在VC++中,主要通过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.

详细可见http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb762494(v=vs.85).aspx

第四个参数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++得到系统特殊文件夹路径  
//http://blog.csdn.net/morewindows/article/details/8745532  
//By MoreWindows( http://blog.csdn.net/MoreWindows )  
#include <windows.h>  
#include <shlobj.h>  
#include <stdio.h>  
int main() 
{    
    printf("    VC++得到系统特殊文件夹路径  \n"); 
    printf(" - http://blog.csdn.net/morewindows/article/details/8745532 -\n"); 
    printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");  
 
    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); 
    } 
    getchar(); 
    return 0; 

// VC++得到系统特殊文件夹路径
 
//By MoreWindows( http://blog.csdn.net/MoreWindows )
#include <windows.h>
#include <shlobj.h>
#include <stdio.h>
int main()

 printf("    VC++得到系统特殊文件夹路径  \n");
 printf(" - http://blog.csdn.net/morewindows/article/details/8745532 -\n");
 printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");

 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);
 }
 getchar();
 return 0;
}
  

 

补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,