1. File Property And CFile
We can get important file properties by making a call to the CFile’s static function GetFileStatus(). This function takes CFileStatus object as a reference param and fills it with the needed data. In this example, we will see how to use the CFileStatus object to fetch some critical file properties.
2. About the Example
The below picture shows the screenshot of example:

When the user clicks the browse button, the example displays file selection dialog. From this dialog, user picks a file name. The example shows the file properties in the relevant user interface items as in the above screen.
3. Creating the Example
Create a dialog based MFC application and name it as FileProp by accepting all the default settings in wizard pages. Once the application is created, the class view looks like below:

The below-shown screen shot will help you in designing the application dialog. The dialog items marked with number 1 are the text boxes. All three check boxes marked as 3 are placed in the group box marked as number 2. Note, the OK button is renamed as close and a browse button marked as 4 is added. That is all; the dialog is ready with the UI elements.

After the UI design, we should assign a control variable for each controls. One can refer the below table:
UI Element | Control Variable Name |
File Name Edit Box | m_ctrl_edit_filename |
Created On Edit Box | m_ctrl_edit_filecreatedon |
Modified On Edit Box | m_ctrl_edit_filemodifiedon |
File Size Edit Box | m_ctrl_edit_filesize |
Read Only Check Box | m_ctrl_chk_readonly |
Hidden Check Box | m_ctrl_chk_hidden |
System Check Box | m_ctrl_chk_system |
To know how to add a control variable watch the video shown in the below link:
4. Pick a File
To select a file in the system, we can use MFC’s common file dialog. Next, we create an Instance of CFileDialog and then display it using the DoModal call. The below picture shows the dialog:

The GetPathName() function of the CFileDialog’s will give us the filename selected by the user with the path. Even though the name states path name, we actually get filename with fully qualified path. Below is the code for reference:
1 2 3 4 5 6 7 8 9 10 |
//Sample 3.0: Get File Properties void CFilePropDlg::OnBnClickedButtonBrowse() { //Sample 3.1: Get Name of the File CFileDialog dlg ( TRUE, NULL, NULL, NULL, "All Files|*.*||" ) ; if ( dlg.DoModal( ) == IDOK ) { m_filename = dlg.GetPathName() ; m_ctrl_edit_filename.SetWindowText(m_filename); } |
5. File Created & Modified Time
We have the file name from the previous section of code. Now we will retrieve the properties of this selected file. As already specified in the introduction, we pass the
CFileStatus object to the
GetStatus function of the CFile
and this call fills the file properties in the CFileStatus structure. In the below code, m_ctime
, m_mtime
members are used to get the file created time as well as file modified time.
These members (m_ctime, m_mtime) are CTime instances and using the Format function of the CTime, the times (i.e.) created time and modified times are formatted in more readable format. Then this formatted time is displayed in the application example. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 3.2: Get the File Status Object CFileStatus status; CFile::GetStatus(m_filename, status); //Sample 3.3: Get File Creation, Modification CString time_formatted; CTime time; time = status.m_ctime; time_formatted = time.Format("%d-%b-%Y %H:%M:%S"); m_ctrl_edit_filecreatedon.SetWindowText(time_formatted); time = status.m_mtime; time_formatted = time.Format("%d-%b-%Y %H:%M:%S"); m_ctrl_edit_filemodifiedon.SetWindowText( time_formatted); |
6. File Attributes & Size
We retrieved the size of the file through the member m_size
. This member is formatted in a CString
object then displayed on the dialog. Below is the code for it:
1 2 3 4 |
//Sample 3.4: Get File Size CString file_size; file_size.Format("%ld", status.m_size ); m_ctrl_edit_filesize.SetWindowText(file_size); |
The Bitwise OR operation collects the file attributes when we make a call to the GetStatus function. To make a check for the particular attributes, we should do Bitwise AND operation. The below enum give the hexadecimal-decimal constant for each attribute:
1 2 3 4 5 6 7 8 9 |
enum Attribute { normal = 0x00, readOnly = 0x01, hidden = 0x02, system = 0x04, volume = 0x08, directory = 0x10, archive = 0x20 }; |
In the
CFileStatus these attribute values are pushed into the member m_attribute
and we test the constant values listed above using the Bitwise AND operation. In the below code, we do such a test and place/remove the check mark on the corresponding checkboxes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 3.5: Get the File Attributes if ( (status.m_attribute & 0x01) == 0x01 ) m_ctrl_chk_readonly.SetCheck(BST_CHECKED); else m_ctrl_chk_readonly.SetCheck(BST_UNCHECKED); if ( (status.m_attribute & 0x02) == 0x02) m_ctrl_chk_hidden.SetCheck(BST_CHECKED); else m_ctrl_chk_hidden.SetCheck(BST_UNCHECKED); if ( (status.m_attribute & 0x04) == 0x04) m_ctrl_chk_system.SetCheck(BST_CHECKED); else m_ctrl_chk_system.SetCheck(BST_UNCHECKED); |
The below video explains and shows how the sample application works:
Source Code : Download
Categories: MFC
Tags: Attribute Enum, CFileDialog, CFileStatus, GetFileStatus, GetStatus