获得程序的路径,linux下取得指定目录所在分区的使用信息,也可以取得指定分区名称的使用信息获得程序的路径,linux下取得指定目录所在分区的使用信息,也可以取得指定分区名称的使用信息
- //获得程序路径
- void GetModulePath(CString &strPath)
- {
- TCHAR drive[_MAX_DRIVE];
- TCHAR dir[_MAX_DIR];
- TCHAR fname[_MAX_FNAME];
- TCHAR ext[_MAX_EXT];
- TCHAR szPathName[MAX_PATH];
- if(GetModuleFileName(NULL, szPathName, MAX_PATH) ==0)
- {
- TRACE(_T("Get module file name error!"));
- }
- _tsplitpath(szPathName, drive, dir, fname, ext );
- strPath = drive;
- strPath += dir;
- }
- //创建目录,实现CreateDirectory函数不能创建子目录的功能
- void CreateDir(LPCTSTR lpszPath)
- {
- CString strPath(lpszPath);
- if(strPath.GetLength() 3)
- return;
- if(strPath.Right(1) != _T("\\"))
- {
- strPath += _T("\\");
- }
- int iPos = -1;
- while((iPos = strPath.Find(_T("\\"),iPos + 1)) >= 0)
- {
- CString strDir((LPCTSTR)strPath, iPos);
- CreateDirectory((LPCTSTR)strDir,NULL);
- }
- }
- //linux下取得指定目录所在分区的使用信息
- //statfs(...)也可以取得指定分区名称的使用信息
- #define B(x,y) (x)*(y.f_bsize/1024)
- //类似于 df -a
- int getpathblocks(const char* path)
- {
- int nret = 0;
- struct statfs st;
- if(statfs(path,&st) == -1)
- {
- nret = -1;
- }
- else
- {
- printf("%-12s%12s%12s%12s\n","FilePath","1K-blocks","Used","Available");
- printf("%-12s%12ld%12ld%12ld\n",path,B(st.f_blocks,st),B(st.f_blocks,st)-B(st.f_bfree,st),B(st.f_bavail,st));
- }
- return nret;
- }
- //类似于 df -i
- int getpathinodes(const char* path)
- {
- int nret = 0;
- struct statfs st;
- if(statfs(path,&st) == -1)
- {
- nret = -1;
- }
- else
- {
- printf("%-12s%12s%12s%12s\n","FilePath","Inodes","IUsed","IFree");
- printf("%-12s%12ld%12ld%12ld\n",path,st.f_files,st.f_files-st.f_ffree,st.f_ffree);
- }
- return nret;
- }