zoukankan      html  css  js  c++  java
  • VC++得到系统特殊文件夹路径

    地址:http://blog.csdn.net/morewindows/article/details/8745532转载请标明出处,谢谢。

    欢迎关注微博:http://weibo.com/MoreWindows      

     

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

           Windows程序来说,得到系统特殊文件夹路径是个非常实用的功能。比如要执行一些系统程序像cmd.exemspaint.exeping.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.

    二.代码示例

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

    // 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;
    }

     

    三.运行结果

    运行结果如下图所示(图片不能打开?请访问)

     

     

    地址:http://blog.csdn.net/morewindows/article/details/8745532转载请标明出处,谢谢。

    欢迎关注微博:http://weibo.com/MoreWindows      


     

  • 相关阅读:
    连接SDE数据库hl3292修改已解决
    ORACLE 10g 安装
    SharpMap相关的一些博客和网站
    Delphi 学习之函数 ②扩展的日期时间操作函数
    Delphi学习之函数 ⑾进制函数及过程
    Delphi学习之函数 ① 扩展的字符串操作函数
    html 初次接触html学习【1】
    Delphi 学习之函数 ③ 扩展的位操作函数
    Delphi学习之函数 ⑨汉字拼音功能函数
    Delphi学习之函数 ④扩展的文件及目录操作函数
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2993607.html
Copyright © 2011-2022 走看看