生活随笔
收集整理的這篇文章主要介紹了
VC++得到系统特殊文件夹路径
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
地址:http://blog.csdn.net/morewindows/article/details/8745532 轉載請標明出處,謝謝。
歡迎關注微博:http://weibo.com/MoreWindows ??????
?
?????? VC++ 得到系統特殊文件夾路徑
?????? 對Windows 程序來說,得到系統特殊文件夾路徑是個非常實用的功能。比如要執行一些系統程序像cmd.exe 、mspaint.exe 、ping.exe 時最好加上絕對路徑(通常為C:\WINDOWS\system32 ),否則有可能會出現找不到指定文件的錯誤。還有要創建桌面快捷方式、啟動菜單快捷方式等等也須要使用系統特殊文件夾路徑。
?????? 在批處理中,要獲取系統特殊文件夾路徑非常簡單。如%SystemDrive% 就能得到系統盤符。在VC++ 中,主要通過SHGetSpecialFolderPath 函數來獲取系統特殊文件夾路徑。下面來看看這個函數的介紹:
?
一.SHGetSpecialFolderPath
函數功能:Retrieves the path of a special folder
函數原型:
BOOL SHGetSpecialFolderPath (
?????? HWND hwndOwner ,
?????? LPTSTR lpszPath ,
?????? int nFolder ,
?????? BOOL fCreate
);
參數說明:
第一個參數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] view plaincopy print?
?? ?? ?? #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++得到系統特殊文件夾路徑
//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 ??????
總結
以上是生活随笔 為你收集整理的VC++得到系统特殊文件夹路径 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。