代码改变世界

Win32 文件(2)

2011-03-28 21:46  Clingingboy  阅读(635)  评论(0编辑  收藏  举报

 

8.6 如何复制文件

CopyFile函数

//复制文件,如果目标文件存在将不覆盖。
if (::CopyFile(strSourcePathName, strTargetPathName, TRUE))
{
    AfxMessageBox(_T("复制文件成功。"));
}
else
{
    AfxMessageBox(_T("复制文件失败。"));
}

 

8.7 如何移动文件

MoveFile函数

//移动文件
if (::MoveFile(strSourcePathName, strTargetPathName))
{
    AfxMessageBox(_T("移动文件成功。"));
}
else
{
    AfxMessageBox(_T("移动文件失败。"));
}

 

8.8 如何删除文件

DeleteFile函数

//删除文件
if(::DeleteFile(strPathName))
{
    AfxMessageBox(_T("删除文件成功。"));
}
else
{
    AfxMessageBox(_T("删除文件失败。"));
}

 

8.9 如何重命名文件

使用CFile::Rename静态方法

//重命名文件
CFile::Rename(strOldPathName, strNewPathName);

AfxMessageBox(_T("重命名文件成功。"));

 

8.10 如何查找文件

用CFileFind 相关操作

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();
}

 

8.11 如何使用Shell操作文件

 

8.12 如何获得应用程序的目录

GetModuleFileName函数

void CDemoDlg::OnGetAppDir() 
{
    TCHAR szFileName[MAX_PATH];

    //获得应用程序的文件全路径和文件名
    if (::GetModuleFileName(NULL, szFileName, MAX_PATH))
    {
        //去掉文件名
        CString strFileName = szFileName;
        int nIndex = strFileName.ReverseFind('\\');
        CString strDirectory = strFileName.Left(nIndex);

        CString strText = _T("");
        strText.Format(_T("应用程序目录:\n%s"), strDirectory);
        AfxMessageBox(strText);
    }
}

 

8.13 如何获得或设置进程的当前目录

GetCurrentDirectory和SetCurrentDirectory方法

void CDemoDlg::OnGetCurDir() 
{
    TCHAR szDirectory[MAX_PATH];

    //获得进程的当前目录
    if (::GetCurrentDirectory(MAX_PATH, szDirectory))
    {
        CString strText = _T("");
        strText.Format(_T("进程的当前目录:\n%s"), szDirectory);
        AfxMessageBox(strText);
    }
}

void CDemoDlg::OnSetCurDir() 
{
    CString strDirectory = _T("C:\\");

    //设置进程的当前目录
    if (::SetCurrentDirectory(strDirectory))
    {
        CString strText = _T("");
        strText.Format(_T("进程的当前目录:\n%s"), strDirectory);
        AfxMessageBox(strText);
    }
}

 

8.14 如何获得Windows目录和System目录

GetWindowsDirectory和GetSystemDirectory函数,即C盘的Windows目录及Windows的System32子目录

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);
    }
}

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);
    }    
}