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

    8.1 如何获得或设置文件的属性

    GetFileAttributes和SetFileAttributes方法

    void CDemoDlg::OnGetFileAttributes() 
    {
        //创建文件夹对话框
        CFolderDialog dlg(NULL, NULL, NULL, BIF_BROWSEINCLUDEFILES);
    
        if (dlg.DoModal() == IDOK)
        {
            //获得文件路径
            CString strPathName = dlg.GetPathName();
    
            //获得文件属性
            DWORD dwFileAttributes = ::GetFileAttributes(strPathName);
    
            CString strFileAttributes = _T("");
    
            if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
            {
                strFileAttributes += _T("无\n");
            }
            if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                strFileAttributes += _T("目录\n");
            }
            if (dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
            {
                strFileAttributes += _T("存档\n");
            }
            if (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
            {
                strFileAttributes += _T("隐藏\n");
            }
            if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
            {
                strFileAttributes += _T("只读\n");
            }
            if (dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
            {
                strFileAttributes += _T("系统\n");
            }
            if (dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)
            {
                strFileAttributes += _T("临时\n");
            }
    
            CString strText = _T("");
            strText.Format(_T("文件属性:\n%s"), strFileAttributes);
            AfxMessageBox(strText);    
        }
    }
    
    void CDemoDlg::OnSetFileAttributes() 
    {
        //创建文件夹对话框
        CFolderDialog dlg(NULL, NULL, NULL, BIF_BROWSEINCLUDEFILES);
    
        if (dlg.DoModal() == IDOK)
        {
            //获得文件路径
            CString strPathName = dlg.GetPathName();
        
            DWORD dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE |
                FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY;
    
            //设置文件属性
            ::SetFileAttributes(strPathName, dwFileAttributes);
    
            CString strFileAttributes = _T("存档\n隐藏\n只读\n");
            
             CString strText = _T("");
             strText.Format(_T("文件属性:\n%s"), strFileAttributes);
             AfxMessageBox(strText);    
        }
    }
    

    8.2 如何获得文件的信息

    用CFile::GetStatus静态方法获取CFileStatus结构

    void CDemoDlg::OnGetFileInfo() 
    {
        //创建文件对话框
        CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | 
            OFN_OVERWRITEPROMPT, _T("所有文件(*.*)|*.*||"));
    
        if (dlg.DoModal() == IDOK)
        {
            //获得文件路径
            CString strPathName = dlg.GetPathName();
    
            //获得文件信息
            CFileStatus FileStatus;
            if (CFile::GetStatus(strPathName, FileStatus))
            {
                CString strText = _T("");
                CString strInfo = _T("");
    
                strInfo.Format(_T("名称:%s\n"), 
                    FileStatus.m_szFullName);
                strText += strInfo;
    
                strInfo.Format(_T("大小:%d字节\n"), 
                    FileStatus.m_size);
                strText += strInfo;
    
                strInfo.Format(_T("创建时间:%d年%d月%d日,%02d:%02d:%02d\n"), 
                    FileStatus.m_ctime.GetYear(),
                    FileStatus.m_ctime.GetMonth(),
                    FileStatus.m_ctime.GetDay(),
                    FileStatus.m_ctime.GetHour(),
                    FileStatus.m_ctime.GetMinute(),
                    FileStatus.m_ctime.GetSecond());
                strText += strInfo;
                strInfo.Format(_T("修改时间:%d年%d月%d日,%02d:%02d:%02d\n"),
                    FileStatus.m_mtime.GetYear(),
                    FileStatus.m_mtime.GetMonth(),
                    FileStatus.m_mtime.GetDay(),
                    FileStatus.m_mtime.GetHour(),
                    FileStatus.m_mtime.GetMinute(),
                    FileStatus.m_mtime.GetSecond());
                strText += strInfo;
                strInfo.Format(_T("访问时间:%d年%d月%d日,%02d:%02d:%02d\n"),
                    FileStatus.m_atime.GetYear(),
                    FileStatus.m_atime.GetMonth(),
                    FileStatus.m_atime.GetDay(),
                    FileStatus.m_atime.GetHour(),
                    FileStatus.m_atime.GetMinute(),
                    FileStatus.m_atime.GetSecond());
                 strText += strInfo;
    
                CString strFileAttributes = _T("");
                if (FileStatus.m_attribute & 0x00)
                {
                    strFileAttributes += _T("无\n");
                }
                if (FileStatus.m_attribute & 0x01)
                {
                    strFileAttributes += _T("只读\n");
                }
                if (FileStatus.m_attribute & 0x02)
                {
                    strFileAttributes += _T("隐藏\n");
                }
                if (FileStatus.m_attribute & 0x04)
                {
                    strFileAttributes += _T("系统\n");
                }
                if (FileStatus.m_attribute & 0x08)
                {
                    strFileAttributes += _T("卷标\n");
                }
                if (FileStatus.m_attribute & 0x10)
                {
                    strFileAttributes += _T("目录\n");
                }
                if (FileStatus.m_attribute & 0x20)
                {
                    strFileAttributes += _T("存档\n");
                }
                strInfo.Format(_T("属性:%s"), strFileAttributes);
                strText += strInfo;
    
                AfxMessageBox(strText);
            }
        }
    }
    

    8.3 如何使用文件对话框

    即CFileDialog的使用

    void CDemoDlg::OnBrowseFile() 
    {
        //文件扩展名
        CString strFilter = _T("所有文件(*.*)|*.*||");
        //创建文件对话框
        CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | 
            OFN_OVERWRITEPROMPT, strFilter);
        //对话框标题
        dlg.m_ofn.lpstrTitle = _T("浏览文件");
    
        if(dlg.DoModal() == IDOK)
        {
            //获得文件路径
            CString strPathName = dlg.GetPathName();
    
            CString strText = _T("");
            strText.Format(_T("%s"), strPathName);
            AfxMessageBox(strText);    
        }
    }
    

    8.4 如何实现文件夹对话框


    MFC本身没有文件夹对话框,可借助SHBrowseForFolder函数来创建对话框

    int CFolderDialog::DoModal()
    {
        int nResult = 0;
    
        m_pidl = SHBrowseForFolder(&m_bi);
    
        if (m_pidl != NULL)
        {
            nResult = IDOK;
        }
        else
        {
            nResult = IDCANCEL;
        }
    
        return nResult;
    } 
    
    CString CFolderDialog::GetPathName()
    {
        CString strPathName = _T("");
        TCHAR szPathName[MAX_PATH];
    
        if(::SHGetPathFromIDList(m_pidl, szPathName))
        {
            strPathName = szPathName;
        }
    
        return strPathName;
    }
    

    8.5 如何在列表框或组合框中显示文件和目录

    使用DlgDirListComboBox和DlgDirList函数

    void CDemoDlg::OnTest() 
    {
        TCHAR szPathName[MAX_PATH] = _T(".");
        UINT nFileType = DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM | DDL_ARCHIVE;
    
        //在组合框中显示文件和目录列表
        DlgDirListComboBox(szPathName, IDC_COMBO, 0, nFileType);
        ((CComboBox*)GetDlgItem(IDC_COMBO))->SetCurSel(0);
    
        //在列表框中显示文件和目录列表
        DlgDirList(szPathName, IDC_LIST, 0, nFileType);
    }
    
  • 相关阅读:
    python中不同文件中函数和类的调用
    python中使用queue实现约瑟夫环(约瑟夫问题)求解
    C语言中几个常用数学计算函数ceil(), floor(), round()的用法
    python中stack在实际中的简单应用之进制转换
    python中stack在实际中的简单应用之平衡符号
    python中两种栈实现方式的性能对比
    python实现stack并测试
    昨天的面试的一点思考
    从历代帝王的的创业经历看哪些人适合创业
    python chr()和ord()的含义和使用方法
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1998141.html
Copyright © 2011-2022 走看看