现在的位置: 首页 > 综合 > 正文

文件和回收站操作(四)

2014年03月05日 ⁄ 综合 ⁄ 共 3566字 ⁄ 字号 评论关闭

 

文件和回收站操作()
本文讲述一些文件的基本操作,例如:文件夹的创建和删除,删除文件到回收站中,清空回收站,搜索文件,查找文件是否存在等。
1.      创建一个对话框工程:FolderAndFile
2.      添加一个按钮为创建文件夹,代码如下:
       charbuf[256];
       ::GetCurrentDirectory(256,buf);
       m_FolderName.GetWindowText(name);
       strcat(buf,"//");
       strcat(buf,name);
      
       if(CreateDirectory(buf,NULL))
       {
              MessageBox("文件夹创建成功!");
              return;
       }
       else
       {
              MessageBox("文件夹创建失败,已经存在!");
              return;
      }
添加一个按钮为删除文件夹,代码如下:
       charbuf[256];
       ::GetCurrentDirectory(256,buf);
       m_FolderName.GetWindowText(name);
 
       strcat(buf,"//");
       strcat(buf,name);
 
       if(RemoveDirectory(buf))
       {
              MessageBox("文件夹删除成功!");
              return;
       }
       else
       {
              MessageBox("文件夹删除失败,文件不存在!");
              return;
      }
其中:m_FolderName为输入要创建文件夹名称的文本框对应变量。
3.      将文件删除到回收站以及清空回收站
添加一个按钮为“浏览文件”,代码如下:
       CFileDialogfile(true,NULL,NULL,OFN_HIDEREADONLY|OFN_HIDEREADONLY,"All Files(*.*)|*.*||",AfxGetMainWnd());
       if(file.DoModal() == IDOK)
       {
              name = file.GetPathName();
              m_BrowseFile.SetWindowText(name);
      }
其中:m_BrowseFile为一个显示浏览到要删除文件的文本框对应变量。
添加一个按钮为“删除”,即删除一个文件到回收站,如下:
       charfilename[100] = "/0";
       strcpy(filename,name);
       strcat(filename,"/0");
       SHFILEOPSTRUCTshfile;
       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);
其中:CStringstrfilename; CStringname;
添加一个按钮为“清空回收站”,代码如下:
       GetWindowLong(m_hWnd,0);
       SHEmptyRecycleBin(m_hWnd,NULL,SHERB_NOCONFIRMATION||SHERB_NOPROGRESSUI||SHERB_NOSOUND);
      MessageBox("回收站已清空!");
4.      搜索文件,添加一个按钮为“浏览”,代码如下:
       BROWSEINFObi;
       charbuffer[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;
       LPITEMIDLISTpList = NULL;
       if((pList=SHBrowseForFolder(&bi))!=NULL)
       {
              charpath[MAX_PATH];
              ZeroMemory(path,MAX_PATH);
              SHGetPathFromIDList(pList,path);
              GetDlgItem(IDC_BROWSE_FILE)->SetWindowText(path);
      }
 
添加一个按钮为“查找”,如下:
       CStringstrpath;
       GetDlgItem(IDC_FILE_NAME_EDIT)->GetWindowText(strfilename);
       GetDlgItem(IDC_BROWSE_FILE)->GetWindowText(strpath);
       FindFile(strpath);
      bstop = FALSE;
   添加一个函数,如下:
voidCFolderAndFileDlg::FindFile(CStringstrPath)
{
       CStringstrTemp;
       if(strPath.Right(1)!= "//")
              strTemp.Format("%s//*.*",strPath);
       else
              strTemp.Format("%s*.*",strPath);
       CFileFindfindfile;
       BOOLbfind = 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);
       inti;
 
       if((i = open(m_fileExit,_O_RDONLY)) < 0)
       {
              MessageBox("文件不存在!");
       }
       else
       {
              MessageBox("文件已经存在!");
                     return;
       }
       _lclose(i);
 
其中:m_fileExit为输入的要判断的文件的文本框控件对应变量。
     要包含:#include "io.h"
#include "fcntl.h"
下次再接着讲一些文件的其他操作。

 

抱歉!评论已关闭.