路徑相關方法 1,獲取各種系統路徑

2021-06-19 21:30:09 字數 3413 閱讀 2825

介紹獲取windows常用的一些系統路徑的方法(當前使用者),如:桌面、我的文件等。

並提供簡單封裝為類pathhelper,供以後工程快捷呼叫。

①獲取系統system32路徑:(c:\windows\system32)

使用函式getsystemdirectory.  cstring版封裝:

static cstring getsysfolder ()

; getsystemdirectory( szpath, 100 ) ;

return cstring(szpath) ;

}

②獲取

系統windows路徑:(c:\windows)

static cstring getwindowsfolder()

; getwindowsdirectory( szpath, 100 ) ;

return cstring(szpath) ;

}

③獲取模組執行所在資料夾(exe所在目錄)

//獲取執行目錄(exe所在目錄)

static cstring getmodulefolder( hmodule hmodule )

; getmodulefilename( hmodule, szpath, max_path ) ;

zeromemory(_tcsrchr(szpath,_t('\\')), _tcslen(_tcsrchr(szpath,_t('\\') ) )*sizeof(tchar)) ;

return cstring(szpath) ;

}

④獲取模組檔案完整路徑(包含exe檔名及字尾)

//獲取執行模組檔案全路徑

static cstring getmodulefilepath( hmodule hmodule )

; getmodulefilename( hmodule, szpath, max_path ) ;

return cstring(szpath) ;

}

⑤上級目錄:

//得到上一級目錄

static cstring getupfolder(cstring strpath)

附:pathhelper 小工具類標頭檔案:

/*

提供簡化的路徑操作:如exe路徑、系統路徑、上級目錄

*/#ifndef path_helper_h

#define path_helper_h

class pathhelper

~pathhelper(){}

//得到上一級目錄

static cstring getupfolder(cstring strpath)

//獲取執行目錄(exe所在目錄)

static cstring getmodulefolder( hmodule hmodule )

; getmodulefilename( hmodule, szpath, max_path ) ;

zeromemory(_tcsrchr(szpath,_t('\\')), _tcslen(_tcsrchr(szpath,_t('\\') ) )*sizeof(tchar)) ;

return cstring(szpath) ;

} //獲取執行模組檔案全路徑

static cstring getmodulefilepath( hmodule hmodule )

; getmodulefilename( hmodule, szpath, max_path ) ;

return cstring(szpath) ;

} static cstring getsysfolder ()

; getsystemdirectory ( szpath, 100 ) ;

return cstring(szpath) ;

} static cstring getwindowsfolder()

; getwindowsdirectory ( szpath, 100 ) ;

return cstring(szpath) ;

}};#endif

使用api: 

bool shgetspecialfolderpath(

hwnd hwndowner,

lptstr lpszpath,

int nfolder,

bool fcreate

);

引數說明:

hwnd hwndowner:如果在乙個對話方塊或messagebox上顯示時,用到的視窗控制代碼。這裡我們單純獲取路徑,一般設null。

lptstr lpszpath : 接收路徑的字串

int nfolder : 微軟定義好的乙個標誌,用到代表要獲取那種目錄。

常見的如下:

csidl_bitbucket   **站     

csidl_controls 控制面板

csidl_desktop windows 桌面desktop

csidl_desktopdirectory desktop的目錄

csidl_drives 我的電腦

csidl_fonts 字型目錄

csidl_nethood 網路上的芳鄰

csidl_network 網路上的芳鄰虛擬目錄

csidl_personal 我的文件

csidl_printers 印表機

csidl_programs 程式組

csidl_recent 最近開啟的文件

csidl_sendto 「傳送到」選單項

csidl_startmenu 任務條啟動選單項

csidl_startup 啟動目錄

csidl_templates 文件模板

具體內容可以參照 shlobj.h 。

bool fcreate : 如果該資料夾不存在,是否建立它。 一般我們不改系統資料夾,就設fasle。

呼叫示例:

char szpath[max_path] = ; 

shgetspecialfolderpath(null, szpath,csidl_desktop, false);

messagebox(szpath);

效果:

獲得各種系統路徑

獲取系統system32路徑 c windows system32 使用函式getsystemdirectory.cstring版封裝 cpp view plain copy static cstring getsysfolder getsystemdirectory szpath,100 retu...

獲取Android各種系統資訊

string phoneinfo cpu abi android.os.build.cpu abi phoneinfo tags android.os.build.tags phoneinfo version codes.base android.os.build.version codes.bas...

laravel各種路徑的獲取方法

若route中有route get home test homecontroller index name test 檢視中的href跳轉 一 區別 最方便,但路由改變時就又要修改了 二 區別 可直接命名使用,不因uri改變而改變,我喜歡用 注 用route需要在route中 name 三 區別 直...