• MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)


    一、文件夹的创建

     1 void CFileOperationDlg::OnButtonMakeFolder() 
     2 {
     3     // TODO: Add your control notification handler code here
     4     UpdateData(TRUE);
     5     CFileFind m_sFileFind;
     6 
     7     if (!m_sFileFind.FindFile(m_FolderName))
     8     {
     9         CreateDirectory(m_FolderName,NULL);
    10     }
    11 }

    二、文件的创建

    1 void CFileOperationDlg::OnButtonMakeFile() 
    2 {
    3     // TODO: Add your control notification handler code here
    4     UpdateData(TRUE);
    5     CFile m_sFile;
    6     m_sFile.Open(m_FolderName + TEXT("\") + m_FileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
    7     m_sFile.SeekToEnd();
    8     m_sFile.Close();
    9 }

    三、文件夹的复制(包括文件的复制)

     1 void CFileOperationDlg::OnButtonCopy() 
     2 {
     3     // TODO: Add your control notification handler code here
     4     UpdateData(TRUE);
     5     CString m_strDestPath;
     6     m_strDestPath = "D:\TestMyself\FileOperation\Debug";
     7     CString m_strSrcPath = "D:\TestMyself\FileOperation\VISTA";
     8     CopyDirectory(m_strSrcPath,m_strDestPath);
     9 }
    10 
    11 BOOL CFileOperationDlg::CopyDirectory(CString strSrcPath,CString strDestPath)
    12 {
    13     CFileFind m_sFileFind;
    14     if (strSrcPath.IsEmpty())
    15     {
    16         OutputDebugString("源文件名为空,无法进行拷贝!");
    17         return FALSE;
    18     }
    19     if (!m_sFileFind.FindFile(strDestPath))
    20     {
    21         CreateDirectory(strDestPath,NULL);//创建目标文件夹
    22     }
    23     CFileFind finder;
    24     CString path;
    25     path.Format("%s/*.*",strSrcPath);
    26     //AfxMessageBox(path);
    27     BOOL bWorking = finder.FindFile(path);
    28     while (bWorking)
    29     {
    30         bWorking = finder.FindNextFile();
    31         //AfxMessageBox(finder.GetFileName());
    32         if (finder.IsDirectory() && !finder.IsDots())//是文件夹 而且 名称不含 . 或 ..  
    33         {
    34             CopyDirectory(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName());//递归创建文件夹+"/"+finder.GetFileName()  
    35         }
    36         else
    37         {//是文件,则直接复制
    38             //AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()  
    39             CopyFile(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName(),FALSE);
    40         }
    41     }
    42 
    43     return TRUE;
    44 }
    45 
    46 CString CFileOperationDlg::GetFilePath()
    47 {
    48     CString m_FilePath;
    49 
    50     GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
    51     m_FilePath.ReleaseBuffer();
    52 
    53     int m_iPosIndex;
    54     m_iPosIndex = m_FilePath.ReverseFind('\');
    55     m_FilePath = m_FilePath.Left(m_iPosIndex+1);
    56 
    57     return m_FilePath;
    58 }
    59 
    60 CString CFileOperationDlg::GetFileName()
    61 {
    62     CString sFileName;
    63     
    64     sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log"); 
    65     
    66     return sFileName; 
    67 }

    四、文件夹的删除

     1 BOOL CFileOperationDlg::DeleteFolder(LPCTSTR lpszPath)
     2 {
     3     int nLength = strlen(lpszPath);
     4     char *NewPath = new char[nLength + 2];
     5     strcpy(NewPath,lpszPath);
     6     NewPath[nLength] = '';
     7     NewPath[nLength + 1] = '';
     8     SHFILEOPSTRUCT FileOp;
     9     ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
    10     FileOp.fFlags = FOF_NOCONFIRMATION;
    11     FileOp.hNameMappings = NULL;
    12     FileOp.hwnd = NULL;
    13     FileOp.lpszProgressTitle = NULL;
    14     FileOp.pFrom = NewPath;
    15     FileOp.pTo = NULL;
    16     FileOp.wFunc = FO_DELETE;
    17     return SHFileOperation(&FileOp) == 0;
    18 }

    五、文件夹的移动(剪切)

     1 BOOL CFileOperationDlg::MoveFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
     2 {
     3     int nLengthFrm = strlen(lpszFromPath);
     4     char *NewPathFrm = new char[nLengthFrm + 2];
     5     strcpy(NewPathFrm,lpszFromPath);
     6     NewPathFrm[nLengthFrm] = '';
     7     NewPathFrm[nLengthFrm + 1] = '';
     8     SHFILEOPSTRUCT FileOp;
     9     ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
    10     FileOp.fFlags = FOF_NOCONFIRMATION ;
    11     FileOp.hNameMappings = NULL;
    12     FileOp.hwnd = NULL;
    13     FileOp.lpszProgressTitle = NULL;
    14     FileOp.pFrom = NewPathFrm;
    15     FileOp.pTo = lpszToPath;
    16     FileOp.wFunc = FO_MOVE;
    17     
    18     return SHFileOperation(&FileOp) == 0;    
    19 }

    六、文件写操作

     1 void CFileOperationDlg::OnButtonOk() 
     2 {
     3     // TODO: Add your control notification handler code here
     4     UpdateData(TRUE);
     5     try
     6     {
     7         CFile m_sFile;
     8         CFileFind m_FileFind;
     9         CString m_sErrorMessage;
    10         //CString m_sFileName = GetFileName(); 
    11         //CString m_sFilePath = GetFilePath(); 
    12         CString m_sCurrentTime = (CTime::GetCurrentTime()).Format("%Y-%m-%d %X");
    13 
    14         if (!m_FileFind.FindFile(m_FolderName))
    15         {
    16             CreateDirectory(m_FolderName,NULL);
    17         }
    18          m_sFile.Open(m_FolderName + TEXT("\") +m_FileName,CFile::modeCreate |CFile::modeNoTruncate| CFile::modeReadWrite |CFile::shareDenyWrite); 
    19 
    20          m_sFile.SeekToEnd();
    21 
    22          if (sizeof(TCHAR) == sizeof(WCHAR))
    23          {
    24              WORD wSignature = 0xFFFF;
    25              m_sFile.Write(&wSignature,2);
    26          }
    27 
    28          m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************")+TEXT("
    ") ;
    29          m_sFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR));
    30 
    31          m_RichText += TEXT("
    ");
    32          m_sFile.Write(m_RichText,m_RichText.GetLength()*sizeof(TCHAR));
    33          m_sFile.Close();
    34     }
    35     catch(CFileException fileException) 
    36     { 
    37         return ; 
    38     }
    39     
    40 }

    以上代码测试通过!!

  • 相关阅读:
    京东采集器100元、求安慰(京东产品、评价(好评、中评、差评)、价格(参考价、京东价)、图片(大图、小图)、图片识别、广告语)
    Oracle数据库创建表ID字段的自动递增
    iv
    oracle 无监听,无法用ip连接问题(ORA12541: TNS无监听程序错误 )
    sql exp
    dockManager1、dockpanl使用笔记
    windows常用控件
    ComboBoxEdit数据绑定
    winform窗体传值
    分组匹配的一个示例
  • 原文地址:https://www.cnblogs.com/wanzaiyimeng/p/4108801.html
Copyright © 2020-2023  润新知