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      


 

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值