Wince6.0 對文件、文件夾的操作


Wince6.0 操作文件CFile

typedef struct _CSiteNameInfo
{
 TCHAR SiteID[4];//序號
 TCHAR SiteName[18];//工地名
}CSiteNameInfo;

CFile mFile;

CSiteNameInfo siteNameData;

 

1.文件的創建與打開

//如果文件存在就打開,不存在則創建

  if (!mFile.Open(L"\\My Documents\\MyFile",CFile::modeCreate|CFile::modeWrite))//創建失敗
  {
   AfxMessageBox(_T("創建數據庫失敗"));
   return;
  }

 

2.關閉文件

mFile.Close();

 

3.寫文件(以結構體爲單元)

   wcscpy_s(siteNameData.SiteID,  L"site" );

   wcscpy_s(siteNameData.SiteName,L"name");

   mFile.Open(L"\\My Documents\\MyFile",CFile::modeCreate|CFile::modeWrite);
   mFile.SeekToEnd();//將指針移到文件末尾
   mFile.Write(&siteNameData,sizeof(siteNameData));
   mFile.Close();

 

4.讀文件(以結構體爲單元)

mFile.Read(&siteNameData,sizeof(siteNameData));

例:讀取整個文件並將其存入數組

CSiteNameInfo siteInfo[10]; 

for(int i = 0; i < 10; i++)

{

   mFile.Read(&siteInfo[i],sizeof(siteInfo[i]));

}

  //關閉數據文件
  mFile.Close();

 

5.查詢文件是否存在

   if(::GetFileAttributes(L"\\My Documents\\MyFile") != 0xFFFFFFFF)
   {
    ::AfxMessageBox(_T("此文件已存在!"));
   }
   else
   {
    ::AfxMessageBox(_T("此文件不存在!"));
   }

 

6.新建目錄

   if (!CreateDirectory(L"\\My Documents\\mine" , NULL))
   {
    AfxMessageBox(_T("創建目錄失敗"));
    return FALSE;
   }

 

7.複製文件內容到另一個新文件

   if(!CopyFile(L"\\My Documents\\old.dat",
    L"\\My Documents\\new.dat",TRUE))//將old.dat文件的信息複製到new.dat文件內
   {
    MessageBox(L"複製失敗!");
    return;
   }

 

8.刪除文件

DeleteFile(L"\\My Documents\\MyFile.dat");

 

9.刪除目錄

  if (!RemoveDirectory(L"\\My Documents\\mine" ))//刪除目錄
  {
   MessageBox(L"刪除目錄失敗!");
   return;
  }

 

以下部分來自CSDN博客,轉載請標明出處:http://blog.csdn.net/armeasy/archive/2010/07/12/5729625.aspx

10.在WinCE下實現將某文件夾下的所有文件(包括文件夾)拷貝到另一個文件夾中.

算法不復雜,簡單實用.(經試驗,此種方法效果不理想)
//szExistingDir:源文件夾
//szNewDir:目標文件夾
//注意:目標文件夾必須要存在,否則該函數將返回FALSE.
BOOL BrowseAndCopy(const CString szExistingDir, const CString szNewDir)

 CString szExistDir;

 CString szAimDir=szNewDir; //保存目標文件夾路徑

 CString szFindDir=szExistingDir; //保存源文件夾路徑

 if(szFindDir.Right(1)!="\\")
 {
  szFindDir+="\\";
  szExistDir=szFindDir;
 }
 szFindDir+="*.*"; //搜索所有文件

 WIN32_FIND_DATA fd;
 HANDLE hFind;
 hFind=FindFirstFile(szFindDir,&fd); //尋找第一個文件
 if(hFind!=INVALID_HANDLE_VALUE)
 {
  do{
   if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) //判斷是否文件夾
   {    
    if(szAimDir.Right(1)!="\\")
    {
     szAimDir+="\\";
    }
    CreateDirectory(szAimDir+fd.cFileName,NULL); //在目標文件夾中創建相應的子文件夾
    BrowseAndCopy(szExistDir+fd.cFileName,szAimDir+fd.cFileName); //採用遞歸查找子文件下的文件
   }
   else
   {     
    if(szAimDir.Right(1)!="\\")
    {
     szAimDir+="\\";
    }
    
    if(CopyFile(szExistDir+fd.cFileName,szAimDir+fd.cFileName,FALSE)==FALSE) //拷貝文件到目標文件夾
    {
     return FALSE;
    }
       
   }

  }while(FindNextFile(hFind,&fd)); //查找是否存在下一個文件
 }
 else
 {
   //源文件夾爲空,返回
  return FALSE;
 } return TRUE;
}

 

 

11:vc中遞歸刪除非空文件夾


/////////////////////////////////////////////////////////////////////////////
// 函數類型: RemoveFileFolder 
// 返 回 值: null
// 功能描述: 遞歸刪除文件夾及內部文件
// 函數作者: 
// 創建日期:
// 參數列表:
// 變量名: strFileFolderName     變量類型:  CString    變量說明: 文件夾名
////////////////////////////////////////////////////////////////////////////
BOOL RemoveFileFolder(CString strFileFolderName)
{
 CFileFind finder;
 // 打開指定的文件夾進行搜索
 BOOL bWorking = finder.FindFile(strFileFolderName + _T("\\") + _T("*.*")); 
 while(bWorking)
 {
  // 從當前目錄搜索文件
  bWorking = finder.FindNextFile();
  CString strFileName = finder.GetFileName();
  CString strSrc = strFileFolderName + L"\\" + strFileName;

  // 判斷搜索到的是不是"."和".."目錄
  if(!finder.IsDots())
  {
   // 判斷搜索到的目錄是否是文件夾
   if(finder.IsDirectory())
   {
    if(!RemoveFileFolder(strSrc)) // 如果是文件夾的話,進行遞歸
    {
     return false;
    } 
   }
   else
   {
    if(!DeleteFile(strSrc))// 如果是文件,進行刪除
    {
     return false;
    }

   }
  }
 } 
 finder.Close();
 if(!RemoveDirectory(strFileFolderName)) //刪除空目錄
 {
  MessageBox(L"刪除目錄失敗!");
  return FALSE;
 }

 return true;
}

 

 

12.vc中複製文件夾到另一個文件夾

/////////////////////////////////////////////////////////////////////////////
// 函數類型: CopyFileFolder 
// 返 回 值: null
// 功能描述: 複製一個文件夾內的文件到另一個文件夾
// 函數作者: 
// 創建日期:
// 參數列表:
// 變 量 名: strFromFile       變量類型:  CString    變量說明:源文件名
// 變 量 名: strToFile         變量類型:  CString    變量說明:要保存到的文件名
////////////////////////////////////////////////////////////////////////////
BOOL CopyFileFolder(CString strSrcPath, CString strDstPath)
{ // 創建目標文件夾

 CreateDirectory(strDstPath,NULL);
 CFileFind finder;
 // 打開指定的文件夾進行搜索
 BOOL bWorking = finder.FindFile(strSrcPath + _T("\\") + _T("*.*")); 
 while(bWorking)
 {
  // 從當前目錄搜索文件
  bWorking = finder.FindNextFile();
  CString strFileName = finder.GetFileName();
  CString strSrc = strSrcPath + L"\\" + strFileName;
  CString strDst = strDstPath + L"\\" + strFileName;

  // 判斷搜索到的是不是"."和".."目錄
  if(!finder.IsDots())
  {
   // 判斷搜索到的目錄是否是文件夾
   if(finder.IsDirectory())
   {
    // 如果是文件夾的話,進行遞歸
    if(!CopyFileFolder(strSrc, strDst)) 
    {
     return false;
    } 
   }
   else
   {
    // 如果是文件,進行復制
    if(!CopyFile(strSrc, strDst, FALSE))
    {
     return false;
    }

   }
  }
 }     
 return true;
}

發佈了82 篇原創文章 · 獲贊 22 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章