zoukankan      html  css  js  c++  java
  • Win32 文件(2)

    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);
        }    
    }
    
  • 相关阅读:
    Delphi中的构造函数的override的问题
    一个很初级的错误 Destructor忘记override导致内存泄露
    WPF 详解模板
    再说WCF Data Contract KnownTypeAttribute
    ADO.NET Data Service
    Using ADO.NET Data Service
    资源:Localization – 本地化
    Dynamic Resource – 动态资源
    应用开发之Linq和EF
    语法之多线程
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1998142.html
Copyright © 2011-2022 走看看