FileCore.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "FileCore.h"
  2. #pragma region 文件
  3. bool FileCore::IsExistTheFileW(wstring filePath)
  4. {
  5. bool isExist = PathFileExistsW(filePath.c_str());
  6. return isExist;
  7. }
  8. bool FileCore::IsExistProductAppPath()
  9. {
  10. wstring installPath = PathCore::smPath_Install + L"\\" + Product::smName_Application;
  11. bool isExist = PathFileExistsW(installPath.c_str());
  12. if (!isExist) {
  13. wstring installPathnoexe = PathCore::smPath_Install + L"\\" + L"PDFReaderProWin";
  14. isExist = PathFileExistsW(installPathnoexe.c_str());
  15. }
  16. return isExist;
  17. }
  18. bool FileCore::SourceFileMoveToDestionPath(wstring sourceFile, wstring destPath)
  19. {
  20. wstring dest = destPath + L"\\" + Product::smName_InstallPackage;
  21. bool issuccess = MoveFileExW(sourceFile.c_str(), dest.c_str(), MOVEFILE_REPLACE_EXISTING);
  22. return issuccess;
  23. }
  24. #pragma endregion
  25. #pragma region 文件夹
  26. wstring FolderCore::GetNewSaveFolder(wstring saveFilePath, wstring oldchar, wstring newchar)
  27. {
  28. while (true)
  29. {
  30. size_t pos = saveFilePath.find(oldchar);
  31. if (pos != wstring::npos)
  32. {
  33. WCHAR pBuf[1] = { L'\0' };
  34. saveFilePath.replace(pos, oldchar.length(), pBuf, 0);
  35. saveFilePath.insert(pos, newchar);
  36. }
  37. else
  38. {
  39. break;
  40. }
  41. }
  42. return saveFilePath;
  43. }
  44. wstring FolderCore::GetOrCreateFolderPath(wstring saveFilePath)
  45. {
  46. wstring newSaveFolder = FolderCore::GetNewSaveFolder(saveFilePath, L"\\", L"/");
  47. BOOL isCreate = FALSE;
  48. if (!PathIsDirectoryW(newSaveFolder.c_str()))
  49. isCreate = CreateDirectory((newSaveFolder.c_str()), NULL);
  50. return newSaveFolder;
  51. }
  52. bool FolderCore::IsExistTheFolderW(wstring folderPath)
  53. {
  54. BOOL isExist = PathFileExistsW(folderPath.c_str());
  55. return isExist;
  56. }
  57. bool FolderCore::IsRootDirectoryW(wstring folderPath)
  58. {
  59. int first2 = folderPath.find(L"\\");
  60. if (first2 + 1 == folderPath.length())
  61. {
  62. return true;
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. }
  69. wstring FolderCore::AddSubFolderFromRootDirW(wstring folderPath, wstring newSubFolderName)
  70. {
  71. if (IsRootDirectoryW(folderPath))
  72. {
  73. int first = folderPath.find(L"\\");
  74. std::wstring newStr = folderPath.substr(0, first + 1);
  75. folderPath = newStr + newSubFolderName;
  76. }
  77. return folderPath;
  78. }
  79. wstring FolderCore::FolderBrowser(HWND m_hWnd)
  80. {
  81. BROWSEINFO bi;
  82. bi.hwndOwner = m_hWnd;
  83. bi.pidlRoot = CSIDL_DESKTOP;//文件夹的根目录,此处为桌面
  84. bi.pszDisplayName = NULL;
  85. bi.lpszTitle = NULL;//显示位于对话框左上部的提示信息
  86. bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;//有新建文件夹按钮
  87. bi.lpfn = NULL;
  88. bi.iImage = 0;
  89. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  90. if (pidl == NULL)
  91. {
  92. return L"";
  93. }
  94. TCHAR strFolder[MAX_PATH];
  95. SHGetPathFromIDList(pidl, strFolder);
  96. std::wstring sFolder(strFolder);
  97. return sFolder;
  98. }
  99. wstring FolderCore::SetSHGetFolderPath(_In_ int csidl)
  100. {
  101. TCHAR szPath[MAX_PATH];
  102. if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szPath)))
  103. {
  104. wstring temp(szPath);
  105. return temp;
  106. }
  107. return L"";
  108. }
  109. void FolderCore::InitAppTempPath()
  110. {
  111. //缓存路劲
  112. wchar_t strTmpDir[MAX_PATH] = { 0 };
  113. GetTempPathW(MAX_PATH, strTmpDir);
  114. wstring temp = strTmpDir;
  115. if (temp.empty() == false && temp != L"")
  116. {
  117. temp = temp + TempFolderName;//Temp缓存文件
  118. }
  119. else
  120. {
  121. temp = SetSHGetFolderPath(CSIDL_PERSONAL) + L"\\" + TempFolderName;//文档
  122. }
  123. BOOL isCreate = True;
  124. if (!PathIsDirectoryW(temp.c_str()))
  125. isCreate = CreateDirectory((temp.c_str()), NULL);
  126. if (isCreate == FALSE)
  127. temp = L"";
  128. /*temp = temp+L"\\";*/
  129. // PathCore::smPath_Temp = AppCore::ReplaceSubStr(temp, L"\\", L"/");
  130. PathCore::smPath_Temp = temp;
  131. isCreate = True;
  132. //安装路径
  133. if (!PathIsDirectoryW(PathCore::smPath_Install.c_str()))
  134. isCreate = CreateDirectory((PathCore::smPath_Install.c_str()), NULL);
  135. if (isCreate)
  136. {
  137. PathCore::smPath_Install = PathCore::smPath_Install + L"\\" + Product::smName_Product;
  138. if (!PathIsDirectoryW(PathCore::smPath_Install.c_str()))
  139. isCreate = CreateDirectory((PathCore::smPath_Install.c_str()), NULL);
  140. PathCore::smPath_DefaultInstall = PathCore::smPath_Install;
  141. }
  142. }
  143. wstring FolderCore::GetAppTempPath()
  144. {
  145. if (PathCore::smPath_Temp == L"")
  146. {
  147. PathCore::smPath_Temp = PathCore::smPath_Install;
  148. }
  149. return PathCore::smPath_Temp;
  150. }
  151. #include <io.h>
  152. void FolderCore::ClearAllTempFolder()
  153. {
  154. RemoveFolder(PathCore::smPath_Temp.c_str());
  155. wstring installPathFile = PathCore::smPath_Install + L"\\" + Product::smName_InstallPackage;
  156. DeleteFile(installPathFile.c_str());
  157. }
  158. void FolderCore::RemoveFolder(wstring path)
  159. {
  160. WIN32_FIND_DATA FindFileData;
  161. wstring s = path + L"\\*.*";
  162. HANDLE hFind = FindFirstFile(s.c_str(), &FindFileData);
  163. if (INVALID_HANDLE_VALUE == hFind)
  164. return;
  165. do
  166. {
  167. if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  168. {
  169. //Todo:暂时不遍历子目录,以下if语句是错的
  170. /*if (FindFileData.cFileName != _T(".") && FindFileData.cFileName != _T("."))
  171. {
  172. wstring subFolder = path + L"\\" + FindFileData.cFileName;
  173. RemoveFolder(subFolder);
  174. }*/
  175. }
  176. else
  177. {
  178. wstring file = path + L"\\" + FindFileData.cFileName;
  179. DeleteFile(file.c_str());
  180. }
  181. } while (FindNextFile(hFind, &FindFileData));
  182. //删除空文件夹:只对空文件夹有作用
  183. RemoveDirectory(path.c_str());
  184. FindClose(hFind);
  185. }
  186. #pragma endregion
  187. #pragma region 磁盘
  188. bool DiskCore::IsEnoughDisk(wstring smPath)
  189. {
  190. auto cdDisk = smPath.substr(0, 1);
  191. std::wstring str_disk_name = cdDisk + L":\\";
  192. DWORD64 qwFreeBytesToCaller = 0;
  193. DWORD64 qwTotalBytes = 0;
  194. DWORD64 qwFreeBytes = 0;
  195. //获取磁盘信息
  196. BOOL bResult = GetDiskFreeSpaceExW(str_disk_name.c_str(),
  197. (PULARGE_INTEGER)&qwFreeBytesToCaller,//空闲空间(字节)
  198. (PULARGE_INTEGER)&qwTotalBytes,//磁盘总容量(字节)
  199. (PULARGE_INTEGER)&qwFreeBytes);//可获得的空闲空间(字节)
  200. if (bResult)
  201. {
  202. //可用空间是否足够1G时
  203. qwFreeBytesToCaller = qwFreeBytesToCaller / (1024 * 1024);
  204. if (Product::unFreeDiskMinSize > qwFreeBytesToCaller)
  205. return false;
  206. }
  207. return true;
  208. }
  209. wstring DiskCore::GetSystemDriveLetter()
  210. {
  211. wchar_t str[MAX_PATH] = { 0 };
  212. GetSystemDirectoryW(str, MAX_PATH);
  213. wstring diskTag = str;
  214. wstring disk;
  215. //避免获取不到系统盘符的可能,那就默认为C盘
  216. if (diskTag == L"")
  217. {
  218. disk = L"C";
  219. }
  220. else
  221. {
  222. disk = diskTag.substr(0, 1);
  223. }
  224. return disk;
  225. }
  226. //isDowndPackgeBefore:安装前。
  227. bool DiskCore::IsMeetInstallPathMaxLength(bool isDowndPackgeBefore)
  228. {
  229. if (isDowndPackgeBefore)
  230. {
  231. wstring installPathFolder = PathCore::smPath_Install + L"\\";
  232. if (installPathFolder.length() > Product::InstallPathMaxLength)
  233. {
  234. return false;
  235. }
  236. }
  237. else
  238. {
  239. wstring installPath = PathCore::smPath_Install + L"\\" + Product::smName_Application;
  240. if (FileCore::IsExistTheFileW(installPath) == false && installPath.length() > Product::InstallPathMaxLength)
  241. {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. #pragma endregion