文件和回收站操作(四)

 

文件和回收站操作 ( )
本文讲述一些文件的基本操作,例如:文件夹的创建和删除,删除文件到回收站中,清空回收站,搜索文件,查找文件是否存在等。
1.      创建一个对话框工程: FolderAndFile
2.      添加一个按钮为创建文件夹,代码如下:
       char buf [256];
       :: GetCurrentDirectory (256, buf );
       m_FolderName . GetWindowText ( name );
       strcat ( buf , "//" );
       strcat ( buf , name );
      
       if ( CreateDirectory ( buf , NULL ))
       {
              MessageBox ( " 文件夹创建成功 !" );
              return ;
       }
       else
       {
              MessageBox ( " 文件夹创建失败,已经存在 !" );
              return ;
      }
添加一个按钮为删除文件夹,代码如下:
       char buf [256];
       :: GetCurrentDirectory (256, buf );
       m_FolderName . GetWindowText ( name );
 
       strcat ( buf , "//" );
       strcat ( buf , name );
 
       if ( RemoveDirectory ( buf ))
       {
              MessageBox ( " 文件夹删除成功 !" );
              return ;
       }
       else
       {
              MessageBox ( " 文件夹删除失败,文件不存在 !" );
              return ;
      }
其中: m_FolderName 为输入要创建文件夹名称的文本框对应变量。
3.      将文件删除到回收站以及清空回收站
添加一个按钮为“浏览文件”,代码如下:
       CFileDialog file ( true , NULL , NULL , OFN_HIDEREADONLY | OFN_HIDEREADONLY , "All Files(*.*)|*.*||" , AfxGetMainWnd ());
       if ( file . DoModal () == IDOK )
       {
              name = file . GetPathName ();
              m_BrowseFile . SetWindowText ( name );
      }
其中: m_BrowseFile 为一个显示浏览到要删除文件的文本框对应变量。
添加一个按钮为“删除”,即删除一个文件到回收站,如下:
       char filename [100] = "/0" ;
       strcpy ( filename , name );
       strcat ( filename , "/0" );
       SHFILEOPSTRUCT shfile ;
       shfile . hwnd = 0;
       shfile . wFunc = FO_DELETE ;
       shfile . pFrom = filename ;
       shfile . pTo = NULL ;
       shfile . fFlags = FOF_ALLOWUNDO ;
       shfile . hNameMappings = NULL ;
       shfile . lpszProgressTitle = NULL ;
      SHFileOperation (& shfile );
其中: CString strfilename ; CString name ;
添加一个按钮为“清空回收站”,代码如下:
       GetWindowLong ( m_hWnd ,0);
       SHEmptyRecycleBin ( m_hWnd , NULL , SHERB_NOCONFIRMATION || SHERB_NOPROGRESSUI || SHERB_NOSOUND );
      MessageBox ( " 回收站已清空 !" );
4.      搜索文件,添加一个按钮为“浏览”,代码如下:
       BROWSEINFO bi ;
       char buffer [ MAX_PATH ];
       ZeroMemory ( buffer , MAX_PATH );
       bi . hwndOwner = GetSafeHwnd ();
       bi . pidlRoot = NULL ;
       bi . pszDisplayName = buffer ;
       bi . lpszTitle = " 选择一个文件夹 " ;
       bi . ulFlags = BIF_EDITBOX ;
       bi . lpfn = NULL ;
       bi . lParam = 0;
       bi . iImage = 0;
       LPITEMIDLIST pList = NULL ;
       if (( pList = SHBrowseForFolder (& bi ))!= NULL )
       {
              char path [ MAX_PATH ];
              ZeroMemory ( path , MAX_PATH );
              SHGetPathFromIDList ( pList , path );
              GetDlgItem ( IDC_BROWSE_FILE )-> SetWindowText ( path );
      }
 
添加一个按钮为“查找”,如下:
       CString strpath ;
       GetDlgItem ( IDC_FILE_NAME_EDIT )-> GetWindowText ( strfilename );
       GetDlgItem ( IDC_BROWSE_FILE )-> GetWindowText ( strpath );
       FindFile ( strpath );
      bstop = FALSE ;
   添加一个函数,如下:
void CFolderAndFileDlg :: FindFile ( CString strPath )
{
       CString strTemp ;
       if ( strPath . Right (1)!= "//" )
              strTemp . Format ( "%s//*.*" , strPath );
       else
              strTemp . Format ( "%s*.*" , strPath );
       CFileFind findfile ;
       BOOL bfind = findfile . FindFile ( strPath );
       while ( bfind )
       {
              bfind = findfile . FindNextFile ();
              if ( strfilename == findfile . GetFileName ())
              {
                     m_filelist . AddString ( findfile . GetFilePath ());
              }
              if ( findfile . IsDirectory ()&&! findfile . IsDots ())
              {
                     FindFile ( findfile . GetFilePath ());
              }
              if ( bstop ) return ;
       }
}
 
其中: m_filelist 为一个显示文件的列表控件对应变量。
5.      判断一个文件是否存在,添加一个按钮“是否存在”,代码如下:
       UpdateData ( TRUE );
       int i ;
 
       if (( i = open ( m_fileExit , _O_RDONLY )) < 0)
       {
              MessageBox ( " 文件不存在 !" );
       }
       else
       {
              MessageBox ( " 文件已经存在 !" );
                     return ;
       }
       _lclose ( i );
 
其中: m_fileExit 为输入的要判断的文件的文本框控件对应变量。
     要包含: #include "io.h"
#include "fcntl.h"
下次再接着讲一些文件的其他操作。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值