开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 1 Fork 0

reesoft/common-cpp

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (2)
master
V1.0-Release
master
分支 (2)
master
V1.0-Release
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (2)
master
V1.0-Release
common-cpp
/
src
/
Utility
/
FileUtility.cpp
common-cpp
/
src
/
Utility
/
FileUtility.cpp
FileUtility.cpp 15.83 KB
一键复制 编辑 原始数据 按行查看 历史
reesoft 提交于 2020年10月11日 10:01 +08:00 . V1.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
// Copyright (c) 2020 Lv Decai, Shenzhen, China.
// Filename: FileUtility.cpp
// Version: 1.0
// Release date: 2020年10月11日
// License: MIT (http://opensource.org/licenses/MIT)
#include "StdAfx.h"
#include <ShlObj.h>
#include "Buffer.h"
#include "DllLoader.h"
#include "FileUtility.h"
#include "StringUtility.h"
#pragma comment(lib, "Version.lib")
NAMESPACE_BEGIN;
// 判断目录是否存在
BOOL FileUtility::DirectoryExists(PCTSTR sPath)
{
if (!StringUtility::ValidString(sPath))
{
return FALSE;
}
CString sDirectory(sPath);
if (sDirectory.GetLength() == 2 && sDirectory[1] == _T(':'))
{
// 单独的盘符要转换为根目录
sDirectory += _T('\\');
}
else
{
// 其他路径,要去掉末尾的反斜线,否则 FindFirstFile 查找的将是目录下的子目录,而不是目录本身
sDirectory.TrimRight(_T('\\'));
}
WIN32_FIND_DATA Win32FindData = { 0 };
HANDLE hFindFile = FindFirstFile(sDirectory, &Win32FindData);
if (hFindFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
FindClose(hFindFile);
return Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
}
// 判断文件是否存在
BOOL FileUtility::FileExists(PCTSTR sPath)
{
if (!StringUtility::ValidString(sPath))
{
return FALSE;
}
WIN32_FIND_DATA Win32FindData = { 0 };
HANDLE hFindFile = FindFirstFile(sPath, &Win32FindData);
if (hFindFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
FindClose(hFindFile);
return (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
}
// 从文件全路径截取目录
CString FileUtility::ExtractDirectory(PCTSTR sPath, BOOL bEndWithBackSlash)
{
CString sPathCopy(sPath);
int iPos = sPathCopy.ReverseFind(_T('\\'));
if (iPos != -1)
{
sPathCopy = sPathCopy.Left(bEndWithBackSlash ? iPos + 1 : iPos);
}
else
{
iPos = sPathCopy.ReverseFind(_T('/'));
if (iPos != -1)
{
sPathCopy = sPathCopy.Left(bEndWithBackSlash ? iPos + 1 : iPos);
}
else
{
sPathCopy.Empty();
}
}
return sPathCopy;
}
// 从文件全路径截取文件名
CString FileUtility::ExtractFilename(PCTSTR sPath, BOOL bIncludeExt)
{
CString sPathCopy = sPath;
int iPos = sPathCopy.ReverseFind(_T('\\'));
if (iPos != -1)
{
sPathCopy = sPathCopy.Mid(iPos + 1);
}
else
{
iPos = sPathCopy.ReverseFind(_T('/'));
if (iPos != -1)
{
sPathCopy = sPathCopy.Mid(iPos + 1);
}
}
if (!bIncludeExt)
{
iPos = sPathCopy.ReverseFind(_T('.'));
if (iPos != -1)
{
sPathCopy = sPathCopy.Left(iPos);
}
}
return sPathCopy;
}
// 从文件名截取扩展名,不含 . 字符
CString FileUtility::ExtractFileExt(PCTSTR sPath)
{
CString sPathCopy = sPath;
int iPos = sPathCopy.ReverseFind(_T('.'));
if (iPos == -1)
{
sPathCopy.Empty();
}
else
{
sPathCopy = sPathCopy.Mid(iPos + 1);
}
return sPathCopy;
}
// 递归创建目录
BOOL FileUtility::CreateDirectoryRecursively(PCTSTR sDirectory)
{
if (!StringUtility::ValidString(sDirectory))
{
return FALSE;
}
if (DirectoryExists(sDirectory))
{
return TRUE;
}
CString sParentDirectory = ExtractDirectory(sDirectory, FALSE);
if (!CreateDirectoryRecursively(sParentDirectory))
{
return FALSE;
}
return CreateDirectory(sDirectory, NULL);
}
// 删除目录及目录中的子目录和文件
BOOL FileUtility::DeleteDirectory(PCTSTR sDirectory)
{
if (!StringUtility::ValidString(sDirectory))
{
return FALSE;
}
CString sDirectoryCopy(sDirectory);
sDirectoryCopy.TrimRight(_T('\\'));
CFileStatus FileStatus;
// 检查状态
if (!CFile::GetStatus(sDirectoryCopy, FileStatus))
{
return FALSE;
}
// 检查属性
if ((FileStatus.m_attribute & CFile::directory) == 0)
{
return FALSE;
}
// 遍历目录下的子目录和文件
CFileFind FileFind;
BOOL bContinue = FileFind.FindFile(sDirectoryCopy + _T("\\*.*"));
while (bContinue)
{
bContinue = FileFind.FindNextFile();
if (FileFind.IsDots())
{
continue;
}
else if (FileFind.IsDirectory())
{
// 递归删除子目录
if (!DeleteDirectory(FileFind.GetFilePath()))
{
return FALSE;
}
}
else
{
// 去除文件的只读、系统、隐藏属性
RemoveFileAttribute(FileFind.GetFilePath(),
FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
// 删除文件
if (!DeleteFile(FileFind.GetFilePath()))
{
return FALSE;
}
}
}
// 关闭打开的目录项
FileFind.Close();
// 去除目录的只读、系统、隐藏属性
RemoveFileAttribute(sDirectoryCopy, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
// 删除空目录
return RemoveDirectory(sDirectoryCopy);
}
// 删除指定目录下匹配模式的所有文件
BOOL FileUtility::DeleteFiles(PCTSTR sDirectory, PCTSTR sPattern, BOOL bRecursively)
{
if (!StringUtility::ValidString(sDirectory))
{
return FALSE;
}
// 取得文件列表
list<CString> lstFile;
GetFileList(lstFile, sDirectory, sPattern, bRecursively);
for (CString& sFilePath : lstFile)
{
// 去除文件的只读、系统、隐藏属性
RemoveFileAttribute(sFilePath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
// 删除文件
if (!DeleteFile(sFilePath))
{
return FALSE;
}
}
return TRUE;
}
// 添加文件属性
BOOL FileUtility::AddFileAttribute(PCTSTR sPath, int iAttributes)
{
DWORD iCurrentAttributes = GetFileAttributes(sPath);
if ((iCurrentAttributes & iAttributes) != 0)
{
return SetFileAttributes(sPath, iCurrentAttributes & ~iAttributes);
}
return FALSE;
}
// 移除文件属性
BOOL FileUtility::RemoveFileAttribute(PCTSTR sPath, int iAttributes)
{
return AddFileAttribute(sPath, ~iAttributes);
}
// 获取文件大小
ULONGLONG FileUtility::GetFileSize(PCTSTR sPath)
{
CFileStatus FileStatus;
if (CFile::GetStatus(sPath, FileStatus))
{
return FileStatus.m_size;
}
return 0;
}
// 获取文件版本号
FileUtility::CFileVersion FileUtility::GetFileVersion(PCTSTR sPath)
{
CFileVersion FileVersion;
DWORD iVersionInfoSize = 0;
iVersionInfoSize = GetFileVersionInfoSize(sPath, &iVersionInfoSize);
if (iVersionInfoSize == 0)
{
return FileVersion;
}
CBuffer Buffer;
Buffer.Resize(iVersionInfoSize * sizeof(TCHAR));
TCHAR* pBuffer = (TCHAR*) Buffer.Get();
if (!GetFileVersionInfo(sPath, 0, iVersionInfoSize, pBuffer))
{
return FileVersion;
}
PDWORD pLanguage = NULL;
UINT iDataLength = 0;
if (!VerQueryValue(pBuffer, _T("\\VarFileInfo\\Translation"), (PVOID*) &pLanguage, &iDataLength))
{
return FileVersion;
}
FileVersion.Language.Format(_T("%04x%04x"), LOWORD(*pLanguage), HIWORD(*pLanguage));
CString sKeyPrefix = _T("\\StringFileInfo\\") + FileVersion.Language + _T("\\");
PCTSTR pString = NULL;
// 出品公司
if (VerQueryValue(pBuffer, sKeyPrefix + _T("CompanyName"), (PVOID*) &pString, &iDataLength))
{
FileVersion.CompanyName = pString;
}
// 文件描述
if (VerQueryValue(pBuffer, sKeyPrefix + _T("FileDescription"), (PVOID*) &pString, &iDataLength))
{
FileVersion.FileDescription = pString;
}
// 文件版本
if (VerQueryValue(pBuffer, sKeyPrefix + _T("FileVersion"), (PVOID*) &pString, &iDataLength))
{
FileVersion.FileVersion = pString;
}
// 内部名称
if (VerQueryValue(pBuffer, sKeyPrefix + _T("InternalName"), (PVOID*) &pString, &iDataLength))
{
FileVersion.InternalName = pString;
}
// 合法版权
if (VerQueryValue(pBuffer, sKeyPrefix + _T("LegalCopyright"), (PVOID*) &pString, &iDataLength))
{
FileVersion.LegalCopyright = pString;
}
// 原名称
if (VerQueryValue(pBuffer, sKeyPrefix + _T("OriginalFilename"), (PVOID*) &pString, &iDataLength))
{
FileVersion.OriginalFilename = pString;
}
// 产品名称
if (VerQueryValue(pBuffer, sKeyPrefix + _T("ProductName"), (PVOID*) &pString, &iDataLength))
{
FileVersion.ProductName = pString;
}
// 产品版本
if (VerQueryValue(pBuffer, sKeyPrefix + _T("ProductVersion"), (PVOID*) &pString, &iDataLength))
{
FileVersion.ProductVersion = pString;
}
return FileVersion;
}
// 获取模块文件路径
// hModule - 打开的模块句柄,NULL 表示获取可执行文件名路径
CString FileUtility::GetModulePath(HMODULE hModule)
{
TCHAR sModuleFileName[MAX_PATH] = { 0 };
GetModuleFileName(hModule, sModuleFileName, MAX_PATH);
return sModuleFileName;
}
// 获取应用程序可执行文件名,不含目录
CString FileUtility::GetAppName(BOOL bIncludeExt)
{
const CString& sAppExePath = GetModulePath(NULL);
return ExtractFilename(sAppExePath, bIncludeExt);
}
// 获取模块文件所在目录
CString FileUtility::GetModuleDirectory(HMODULE hModule, BOOL bEndWithBackSlash)
{
return ExtractDirectory(GetModulePath(hModule), bEndWithBackSlash);
}
// 获取应用程序可执行文件所在目录
CString FileUtility::GetAppDirectory(BOOL bEndWithBackSlash)
{
return GetModuleDirectory(NULL, bEndWithBackSlash);
}
// 获取特殊目录
CString FileUtility::GetSpecialDirectory(int iType, BOOL bEndWithBackSlash)
{
TCHAR sDirectory[MAX_PATH] = { 0 };
SHGetSpecialFolderPath(NULL, sDirectory, iType, FALSE);
if (bEndWithBackSlash)
{
_tcscat(sDirectory, _T("\\"));
}
return sDirectory;
}
// 在指定目录及其各级父目录下查找文件,返回存在的最长的文件路径
// 返回值:如果找到文件,返回存在的最长的文件路径,否则返回空字符串
CString FileUtility::FindFile(CString sSearchDirectory, CString sRelativeFilePath)
{
sSearchDirectory.TrimRight(_T("\\"));
while (!sSearchDirectory.IsEmpty())
{
CString sFilePath = sSearchDirectory + _T("\\") + sRelativeFilePath;
if (FileUtility::FileExists(sFilePath))
{
return sFilePath;
}
sSearchDirectory = FileUtility::ExtractDirectory(sSearchDirectory, FALSE);
}
return _T("");
}
// 在 Path 环境变量的各个目录下查找文件
// 返回值:如果找到文件,返回第一存在的文件路径,否则返回空字符串
CString FileUtility::FindFileInPathEnvironment(PCTSTR sRelativeFilePath)
{
CString sPathList = StringUtility::GetEnvironmentVariable(_T("Path"));
list<CString> lstPath;
StringUtility::Split(sPathList, lstPath, _T(";"));
for (CString& sDirectory : lstPath)
{
CString sFilePath;
if (sDirectory.Right(1) == _T("\\"))
{
sFilePath = sDirectory + sRelativeFilePath;
}
else
{
sFilePath = sDirectory + _T("\\") + sRelativeFilePath;
}
if (FileExists(sFilePath))
{
return sFilePath;
}
}
return _T("");
}
// 把目录添加到 Path 环境变量中
BOOL FileUtility::AddDirectoryToPathEnvironment(PCTSTR sDirectory)
{
CString sPathList = StringUtility::GetEnvironmentVariable(_T("Path"));
list<CString> lstPath;
StringUtility::Split((PCTSTR) sPathList, lstPath, _T(";"));
if (find(lstPath.begin(), lstPath.end(), sDirectory) == lstPath.end())
{
return StringUtility::SetEnvironmentVariable(_T("Path"), sPathList + _T(";") + sDirectory);
}
return FALSE;
}
// 把目录批量添加到 Path 环境变量中
BOOL FileUtility::AddDirectoryToPathEnvironment(const list<CString>& lstDirectory)
{
CString sPathList = StringUtility::GetEnvironmentVariable(_T("Path"));
list<CString> lstPath;
StringUtility::Split(sPathList, lstPath, _T(";"));
CString sAppendString;
for (auto& sDirectory : lstDirectory)
{
if (find(lstPath.begin(), lstPath.end(), sDirectory) == lstPath.end())
{
sAppendString += _T(";") + sDirectory;
}
}
if (!sAppendString.IsEmpty())
{
return StringUtility::SetEnvironmentVariable(_T("Path"), sPathList + sAppendString);
}
return FALSE;
}
// 获取指定目录下的文件列表
void FileUtility::GetFileList(list<CString>& lstFile, PCTSTR sDirectory, PCTSTR sPattern, BOOL bRecursively)
{
if (!DirectoryExists(sDirectory))
{
return;
}
CString sDirectoryCopy(sDirectory);
if (sDirectoryCopy.Right(1) != _T("\\"))
{
sDirectoryCopy += _T("\\");
}
CFileFind FileFind;
BOOL bContinue = FileFind.FindFile(sDirectoryCopy + sPattern);
while (bContinue)
{
bContinue = FileFind.FindNextFile();
if (!FileFind.IsDirectory())
{
lstFile.push_back(FileFind.GetFilePath());
}
}
if (bRecursively)
{
FileFind.Close();
bContinue = FileFind.FindFile(sDirectoryCopy + _T("*.*"));
while (bContinue)
{
bContinue = FileFind.FindNextFile();
if (FileFind.IsDirectory() && !FileFind.IsDots())
{
GetFileList(lstFile, FileFind.GetFilePath(), sPattern, bRecursively);
}
}
}
}
// 比较两个文件的修改时间,如果相等返回 0,如果文件 1 早于文件 2 返回 -1,如果文件 1 晚于文件 2 返回 1
int FileUtility::CompareFileModificationTime(PCTSTR sFilePath1, PCTSTR sFilePath2)
{
CFileStatus FileStatus1;
CFileStatus FileStatus2;
if (CFile::GetStatus(sFilePath1, FileStatus1))
{
if (CFile::GetStatus(sFilePath2, FileStatus2))
{
if (FileStatus1.m_mtime < FileStatus2.m_mtime)
{
return -1;
}
else if (FileStatus1.m_mtime == FileStatus2.m_mtime)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 1;
}
}
else if (CFile::GetStatus(sFilePath2, FileStatus2))
{
return -1;
}
else
{
return 0;
}
}
// 注册 COM
BOOL FileUtility::RegisterCom(PCTSTR sPath)
{
CDllLoader DllLoader(sPath);
if (!DllLoader.IsValid())
{
return FALSE;
}
FARPROC DllRegisterServer = DllLoader.GetProc("DllRegisterServer");
if (DllRegisterServer == NULL)
{
return FALSE;
}
try
{
return SUCCEEDED(DllRegisterServer());
}
catch (...)
{
return FALSE;
}
}
// 反注册 COM
BOOL FileUtility::UnregisterCom(PCTSTR sPath)
{
CDllLoader DllLoader(sPath);
if (!DllLoader.IsValid())
{
return FALSE;
}
FARPROC DllUnregisterServer = DllLoader.GetProc("DllUnregisterServer");
if (DllUnregisterServer == NULL)
{
return FALSE;
}
try
{
return SUCCEEDED(DllUnregisterServer());
}
catch (...)
{
return FALSE;
}
}
// 从文件读取内容到缓冲区
BOOL FileUtility::ReadFileContent(PCTSTR sPath, void* pBuffer, UINT iBufferSize)
{
if (!StringUtility::ValidString(sPath)
|| pBuffer == NULL
|| iBufferSize == 0)
{
return FALSE;
}
try
{
CFile File(sPath, CFile::modeRead);
UINT iFileSize = (UINT) File.GetLength();
if (iFileSize > iBufferSize)
{
iFileSize = iBufferSize;
}
File.Read(pBuffer, iFileSize);
return TRUE;
}
catch (CFileException* e)
{
e->Delete();
}
return FALSE;
}
// 把缓冲区中的内容写入文件
BOOL FileUtility::WriteFileContent(PCTSTR sPath, PCVOID pBuffer, UINT iBufferSize)
{
if (!StringUtility::ValidString(sPath)
|| pBuffer == NULL
|| iBufferSize == 0)
{
return FALSE;
}
try
{
CFile File(sPath, CFile::modeCreate + CFile::modeWrite);
File.Write(pBuffer, iBufferSize);
return TRUE;
}
catch (CFileException* e)
{
e->Delete();
}
return FALSE;
}
// 从文件读取内容
BOOL FileUtility::ReadFileContent(PCTSTR sPath, string& sContent)
{
try
{
CFile File(sPath, CFile::modeRead);
UINT iFileSize = (UINT) File.GetLength();
sContent.resize(iFileSize);
File.Read((void*)sContent.data(), iFileSize);
return TRUE;
}
catch (CFileException* e)
{
e->Delete();
}
return FALSE;
}
// 向文件写入内容
BOOL FileUtility::WriteFileContent(PCTSTR sPath, const string& sContent)
{
if (!StringUtility::ValidString(sPath)
|| sContent.empty())
{
return FALSE;
}
try
{
CFile File(sPath, CFile::modeCreate + CFile::modeWrite);
File.Write(sContent.c_str(), (UINT)sContent.size());
return TRUE;
}
catch (CFileException* e)
{
e->Delete();
}
return FALSE;
}
// 向文件尾部追加内容
BOOL FileUtility::AppendFileContent(PCTSTR sPath, PCVOID pContent, UINT iSize)
{
if (!StringUtility::ValidString(sPath)
|| pContent == NULL
|| iSize == 0)
{
return FALSE;
}
try
{
CFile File(sPath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
File.SeekToEnd();
File.Write(pContent, iSize);
return TRUE;
}
catch (CFileException* e)
{
e->Delete();
}
return FALSE;
}
// 拷贝文件夹内的文件及其子文件夹到目标文件夹
BOOL FileUtility::CopyDirectory(CString sSrcDirectory, CString sDestDirectory)
{
sSrcDirectory.TrimRight(_T("\\"));
if (!DirectoryExists(sSrcDirectory))
{
return FALSE;
}
if (!DirectoryExists(sDestDirectory))
{
CreateDirectoryRecursively(sDestDirectory);
}
if (sDestDirectory.Right(1) != _T("\\"))
{
sDestDirectory += _T("\\");
}
CFileFind FileFind;
BOOL bContinue = FileFind.FindFile(sSrcDirectory + _T("\\*.*"));
while (bContinue)
{
bContinue = FileFind.FindNextFile();
if (FileFind.IsDirectory())
{
if (!FileFind.IsDots())
{
// 递归复制子目录
CopyDirectory(FileFind.GetFilePath(), sDestDirectory + FileFind.GetFileName());
}
}
else
{
// 复制文件
CopyFile(FileFind.GetFilePath(), sDestDirectory + FileFind.GetFileName(), FALSE);
}
}
return TRUE;
}
NAMESPACE_END;
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

C++ 工具代码包
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/reesoft/common-cpp.git
git@gitee.com:reesoft/common-cpp.git
reesoft
common-cpp
common-cpp
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /