取得目录和取得操作系统盘符以及获取系统盘可用空间

来源:互联网 发布:unity3d 旋转 编辑:程序博客网 时间:2024/05/18 02:09

1.取得当前目录

[cpp] view plain copy
  1. char        szModulePath [_MAX_PATH];   /* Path of Module */      
  2. // Get current module handle  
  3.     HMODULE module = GetModuleHandle(0);  
  4.     // Get current file path  
  5.     GetModuleFileName(module, szModulePath, sizeof(szModulePath));  
  6.     CString strPath = szModulePath;  
  7.     strPath = strPath.Left(strPath.ReverseFind(_T('//')));  
  8.     lstrcpy(szModulePath, strPath.GetBuffer(strPath.GetLength()));  

 

2.取得操作系统盘符

[cpp] view plain copy
  1. TCHAR sysDir[128];  
  2. GetSystemDirectory(sysDir, 128 * sizeof(TCHAR));  
  3. CString sysDisk = sysDir[0];  
  4. sysDisk.MakeUpper();  

3.取得操作系统盘可用空间的情况


// 判断系统磁盘可用空间的情况。返回可用空间大小情况: TRUE 大于1GB,FALSE 小于1GB。 BOOL CheckSystemDiskFreeSpace( ){TCHAR sysDir[128];  GetSystemDirectory(sysDir, 128 * sizeof(TCHAR));  BOOL bret;ULONGLONG ullMaxSpace = 0;TCHAR szRootPath[] = { TCHAR('A'), ':', '\\', 0 };//带根目录标记的磁盘符号szRootPath[0] = sysDir[0];DWORD dwSectorsPerCluster = 0;//每簇中扇区数DWORD dwBytesPerSector = 0;//每扇区中字节数DWORD dwFreeClusters = 0;//剩余簇数DWORD dwTotalClusters = 0;//总簇数if (GetDiskFreeSpace(szRootPath, &dwSectorsPerCluster, &dwBytesPerSector,&dwFreeClusters, &dwTotalClusters)){ullMaxSpace = ULONGLONG(dwFreeClusters)*ULONGLONG(dwSectorsPerCluster)*ULONGLONG(dwBytesPerSector);if (ullMaxSpace > 1024*1024*1024){return TRUE;}}return FALSE;}