//获得Windows目录

 

void CDemoDlg::OnGetWinDir()

{

       TCHAR szDirectory[MAX_PATH];

 

       //获得Windows目录

       if (::GetWindowsDirectory(szDirectory, MAX_PATH) > 0)

       {

              CString strText = _T("");

              strText.Format(_T("Windows目录:\n%s"), szDirectory);

              AfxMessageBox(strText);

       }

}

 

 

//获得System目录

void CDemoDlg::OnGetSysDir()

{

       TCHAR szDirectory[MAX_PATH];

 

       //获得System目录

       if (::GetSystemDirectory(szDirectory, MAX_PATH) > 0)

       {

              CString strText = _T("");

              strText.Format(_T("System目录:\n%s"), szDirectory);

              AfxMessageBox(strText);

       }     

}

 

 

//创建目录

void CDemoDlg::OnCreateDir()

{

       CString strDirectory = _T("Demo");

 

       //查找文件

       CFileFind finder;

       if (finder.FindFile(strDirectory))

       {

              finder.FindNextFile();

              if (finder.IsDirectory())

              {

                     AfxMessageBox(_T("目录已经存在。"));

              }

              else

              {

                     AfxMessageBox(_T("目录名与现有的文件名重名。"));

              }

       }

       else

       {

              //创建目录

              if (::CreateDirectory(strDirectory, NULL))

              {

                     AfxMessageBox(_T("创建目录成功。"));

              }

              else

              {

                     AfxMessageBox(_T("创建目录失败。"));

              }

       }

}

 

 

查找文件

void CDemoDlg::OnFindFile()

{

       CListBox* pListBox = (CListBox*)GetDlgItem(IDC_FILELIST);

       pListBox->ResetContent();

 

       //从当前目录开始查找文件

       CString strFileName = _T(".");

       Find(strFileName);

}

void CDemoDlg::Find(LPCTSTR lpszFileName)

{

       CString strWildcard = lpszFileName;

       strWildcard += _T("\\*.*");

 

       CFileFind finder;

       BOOL bFind = FALSE;

 

       //查找文件

       bFind = finder.FindFile(strWildcard);

       while (bFind)

       {

              //查找下一个文件

              bFind = finder.FindNextFile();

 

              //判断找到文件的是否包含"."或".."

              if (finder.IsDots())

              {

                     continue;

              }

 

              //获得找到文件的名称

              if (finder.IsDirectory())

              {

                     //找到文件的路径

                     CString strFilePath = finder.GetFilePath();

                     //递归查找文件

                     Find(strFilePath);

              }

 

              //获得找到文件的名称

              CString strFileName = finder.GetFileName();

 

              CListBox* pListBox = (CListBox*)GetDlgItem(IDC_FILELIST);

              pListBox->AddString(strFileName);

       }

 

       //结束查找

       finder.Close();

}

 

创建临时文件名并在目录中创建文件

void COpoplkDlg::OnButton5()

{

       TCHAR szPathName[MAX_PATH];

       TCHAR szFileName[MAX_PATH];

      

       //获得临时文件目录

       if (!::GetTempPath(MAX_PATH, szPathName))

       {

              return;

       }

      

       //创建临时文件名并在目录中创建文件

       if (!::GetTempFileName(szPathName, _T("~ex"), 0, szFileName))

       {

              return;

       }

      

       CString strText = _T("");

       strText.Format(_T("临时文件:\n%s"), szFileName);

       AfxMessageBox(strText);

      

}