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      


     

  • 相关阅读:
    Microsoft.Office.Interop.Excel, Version=12.0.0.0版本高于引用的程序集(已解决)
    关于dubbo的provider和consumer都配置timeout超时时间的情况
    cas单点登录-CAS5.1.3 overlay服务器搭建(二)
    cas单点登录-https的配置(一)
    mysql5.6性能优化
    jvm面试题
    关于Class.getResource和ClassLoader.getResource的路径问题
    构建流式计算卖家日志系统应用实践
    在分布式系统里看CAP定理
    bootstrap插件(对话框)bootbox参数和自定义弹出框宽度设置
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2993607.html
Copyright © 2011-2022 走看看