Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Getting and Setting File Attributes

Author: DevASP
Download Source Code :

In this article I will describe the attributes of files such as archive, directory, hidden, normal, read only, system, temporary. These all attributes are in the Boolean form(means a file is in hidden mode or not etc).

Beside this I will also discuss that how to get the size of a file, Get the full path of a file.All these Information will be get or Set by OS System services.

Get the File Size

GetFileSize is the API in Windows System service which provide us the size of file in bytes or 0xFFFFFFFF if there is an error. FAT supports only 32 bit size (the maximum size of a file can be represented by 32 bits) But the NTFS file system is a 64 bit file system , GetFileSize()will return the response in 64 bits.The signature of GetFileSize looks like this…



DWORD GetFileSize(

   HANDLE file,

   LPDWORD fileSizeHigh);



The low-order 32 bits of size information come from the return value, while the high-order 32 bits come from the fileSizeHigh parameter when you pass in a pointer to a DWORD. You can also pass in NULL for this parameter if you are not interested in receiving the high-order 32 bits of information.We can write code to get the file size like this



HANDLE fileHandle;

            char filename[]="TestFile.txt";

            DWORD sizeLo, sizeHi;


            // open the file

            fileHandle = CreateFile(filename, GENERIC_READ,

                        FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

            if (fileHandle == INVALID_HANDLE_VALUE)


                        MessageBox("Cant of file");




            // report file size



                        sizeLo = GetFileSize(fileHandle, &sizeHi);

                        char buffer[200];

                        sprintf(buffer,"Hi byte size is%d and Low byte Size is%d",sizeHi,sizeLo);





The above code will show the file size in bytes in the low bytes .You can also match this size by Getting the properties of the file by right click and match the size it will be same.

Getting File Attributes

Each file is associated with some attribute bits which contain special information about the file. we can view these attributes by the file manager by selecting the file and right click on it and then select properties.By using windows System services you can get these attributes by this GetFileAttributes function.Signature of this function looks like this.


DWORD GetFileAttributes(  LPTSTR fileName);


Returns the attribute bits in a DWORD, or 0xFFFFFFFF on error.

The system returns not only the four standard bits seen in the File Manager ( read only, system, and hidden), but also bits indicating that the file name is actually a directory, as well as a temporary status bit to mark temporary files.This function will show a message box against the each attribute which is set (means true).



void ShowAttributes(DWORD attributes)


   if (attributes & FILE_ATTRIBUTE_ARCHIVE)

                  MessageBox("File is Archived");     

            if (attributes & FILE_ATTRIBUTE_DIRECTORY)

                  MessageBox("File is Directory");     

            if (attributes & FILE_ATTRIBUTE_HIDDEN)

                  MessageBox("File is Hidden");     

            if (attributes & FILE_ATTRIBUTE_NORMAL)

                  MessageBox("File is Normal");     

            if (attributes & FILE_ATTRIBUTE_READONLY)

                  MessageBox("File is Read Only");     

            if (attributes & FILE_ATTRIBUTE_SYSTEM)

                  MessageBox("File is system file");     

            if (attributes & FILE_ATTRIBUTE_TEMPORARY)

                 MessageBox("File is temporary ");    



The code which uses this function to Get the actual file attributes is looks like this


char filename[]="TestFile.txt";

            DWORD attributes;


            attributes = GetFileAttributes(filename);



Change the attributes of the TestFile.txt and then execute the program and try it out…

Setting File Attributes

We can set the file attribute by the Windows System service SetFileAttributes().This function accepts a file name and one or more attributes constants, and returns a Boolean value indicating success or failure.The signature of this function looks like this


BOOL SetFileAttributes(LPTSTR fileName, DWORD attributes);


Returns TRUE on success

For example we can Set the file as Hidden and read-only as


success = SetFileAttributes("TestFile.txt",




Generally those are the only two attributes you will want to set. The other bits, for example, the directory bit, are set automatically by system calls when appropriate, and should not be altered.

Getting All File Information

The one windows system service which provides all the information about a file for example its all attributes, size of the file, Timing information (in which Last access time, Last modified time ,and file creation time) is GetFileInformationByHandle.

This single system service provides all the above mentioned information about files.The signature of this function looks like this..


BOOL GetFileInformationByHandle(

   HANDLE file,



Returns TRUE on success.

All this information is come back in a structure which contains some other things beside this.We can use this function in this way.


char filename[]="TestFile.txt";

            HANDLE fileHandle;

            BOOL success;



            fileHandle = CreateFile(filename, GENERIC_READ,

                        FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

            if (fileHandle == INVALID_HANDLE_VALUE)


                        MessageBox("Cant open file");





                        success = GetFileInformationByHandle(fileHandle,


                        if (success)




                                    DisplayTime(info.ftLastWriteTime,"Last write time");

                                    DisplayTime(info.ftCreationTime,"creation time");

                                    DisplayTime(info.ftLastAccessTime,"Last Acess time");

                                    char buffer[200];

                                    sprintf(buffer,"Low byte Size is%d",info.nFileSizeLow);





This code will shows u a lot of attributes

I have also used a function in this code to show time in proper format.


void DisplayTime(FILETIME fileTime, char* DescOfTime)


            FILETIME ft;

            SYSTEMTIME systemtime;

            FileTimeToLocalFileTime(&fileTime, &ft);

            FileTimeToSystemTime(&ft, &systemtime);

            char temp[150];


            MessageBox(temp , DescOfTime , MB_OK); 




FILETIME is a structure that contains two 32-bit values. The 64 bits together represent the number of 100-nanosecond time increments that have passed since January 1, 1601. The FileTimeToLocalTime and FileTimeToSystemTime functions convert the 64-bit value to local time and then to a form suitable for output.

Article Comments
This Ariticle is very useful. It help to solve my problem. It Not Only Provide information abuot api but also about how to Implement them,

Posted on 1/28/2008 3:56:11 AM by Manisha khare


Posted on 1/28/2008 3:56:44 AM by Manisha Khare

Add Article Comment:
Name :
Email Address :
Comments :
<< How we can Find total age in a web application.

Disclaimer - Privacy
© 2002-2017