打开文件对话框,多选与单选,检测文件是否存在

单选文件,

1 char szCurrentWorkPath[256] = {0};
2 GetCurrentDirectory(256, szCurrentWorkPath);
3 CFileDialog dlg(TRUE);
4 CString cszFileFullName;
5 CString cszFileName;
6 if (dlg.DoModal() == IDOK)
7 {
8 cszFileName = dlg.GetFileName();
9 cszFileFullName = dlg.GetPathName();
10 }
11 else
12 {
13 return ;
14 }
15
16 SetCurrentDirectory(szCurrentWorkPath);

 

多选文件 并带过滤器

1 void CAutomateSignDlg::OnFileAddguradfiles()
2 {
3 // TODO: Add your command handler code here
4   char szCurrentWorkPath[256] = {0};
5 GetCurrentDirectory(256, szCurrentWorkPath);
6 CFileDialog dlg(TRUE,NULL,NULL, OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, "Guard file or Zip file (*.grd;*.zip)|*.grd;*.zip|all Files (*.*)|*.*||");
7 if (IDOK == dlg.DoModal())
8 {
9 POSITION pos;
10 for(pos=dlg.GetStartPosition();pos;)
11 {
12 CString str;
13 str=dlg.GetNextPathName(pos);
14 m_ListBoxGuradFiles.AddString(str); //得到每个文件的路径
15   }
16 UpdateData(FALSE);
17 }
18
19 SetCurrentDirectory(szCurrentWorkPath);
20 }

 

目录是否存在的检查:

1 BOOL FolderExist(CString strPath)
2 {
3 WIN32_FIND_DATA wfd;
4 BOOL rValue = FALSE;
5 HANDLE hFind = FindFirstFile(strPath, &wfd);
6 if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
7 {
8 rValue = TRUE;
9 }
10 FindClose(hFind);
11 return rValule;
12 }

 

文件存在性检查:

1 BOOL FileExist(CString strFileName)
2 {
3 CFileFind fFind;
4 return fFind.FindFile(strFileName);
5 }

 

创建目录:

 

1 BOOL CreateFolder(CString strPath)
2 {
3 SECURITY_ATTRIBUTES attrib;
4 attrib.bInheritHandle = FALSE;
5 attrib.lpSecurityDescriptor = NULL;
6 attrib.nLength =sizeof(SECURITY_ATTRIBUTES);
7 //上面定义的属性可以省略。 直接return ::CreateDirectory( path, NULL); 即可
8   return ::CreateDirectory( strPath, &attrib);
9 }

 

posted on 2010-06-22 22:15  Imagination  阅读(854)  评论(0编辑  收藏  举报

导航