Click here to Skip to main content
15,881,600 members
Articles / Desktop Programming / MFC
Article

File Information using C#

Rate me:
Please Sign up or sign in to vote.
2.72/5 (24 votes)
3 Feb 20032 min read 227.9K   2.7K   72   12
Getting information of Files and Directories

Image 1

Introduction

One of the rich experiences in working with .NET is a huge collection of Base Class Libraries. The .NET Framework class library is a library of classes, interfaces, and value types that are included in the Microsoft .NET Framework SDK. This library provides access to system functionality and is designed to be the foundation on which .NET Framework applications, components, and controls are built.

Details

System.IO namespace contains types that allow synchronous and asynchronous reading and writing on data streams and files.

To parse information of lots of files and directories, the FileSystemInfo class comes in handy. It is an abstract class and contains members (methods, properties …) that are common to file and directory manipulation. A FileSystemInfo object can represent either a file or a directory. The implementation of this abstract class is done in FileInfo and DirectoryInfo class. The Members would take full path (absolute or relative) or UNC path if they require path as one of the parameters.

The FileInfo class and DirectoryInfo class extends FileSystemInfo class and each add some more members to themselves for specific operations. The FileInfo class provides instance methods for the creation, copying, deletion, moving, and opening of files, and provides members to create FileStream objects. The DirectoryInfo class provides instance methods for creating, moving, and enumerating through directories and subdirectories.

List of members of FileInfo Class, I have included only a few , for more information refer to .NET SDK.

CreationTimeGet or sets the creation time of the current FileSystemInfo Object.
Directoryget the instance of the parent directory
DirectoryNameGet a string representing the directory's full path
ExistsGet a value indication whether a file exists
FullNameGets the full path of the directory or file
LastAccessTimeGets or sets the time the current file or directory was last accessed.
LastWriteTimeGets or sets the time when the current file or directory was last written to.
NameGets the name of the file

Using the code

This Sample code shows how to use FileInfo and DirectoryInfo Classes.

C#
protected void ProcessFile(string PathName)
{
    try
    {
        FileInfo TheFile = new FileInfo(PathName);
        if (TheFile.Exists)
        {
            DirectoryInfo Parent = TheFile.Directory;
            ListContentsOfFolder(Parent);
            DisplayFileInfo(TheFile);
            return;
        }
        throw new FileNotFoundException();
    }
    catch(FileNotFoundException )
    {
        tbFolder.Text = PathName;
        listFileItems.Items.Add(
             "This file or folder does not exist");
    }
    catch(Exception e)
    {
        tbFolder.Text = PathName;
        listFileItems.Items.Add("Error Processing");
        listFileItems.Items.Add(e.Message);
    }
    finally
    {
        ListItems = false;
    }
}
The above sample code shows how to get the information of the file and display the contents of the file.
C#
protected void ProcessFolder(string PathName)
        {
            try
            {
                DirectoryInfo TheFolder = new DirectoryInfo(PathName);
                if (TheFolder.Exists)
                {
                    ListContentsOfFolder(TheFolder);
                    return;
                }

                throw new FileNotFoundException();
            }
            catch(FileNotFoundException )
            {
                tbFolder.Text = PathName;
                listFolderItems.Items.Add(
                     "This folder or folder does not exist");
            }
            catch(Exception e)
            {
                tbFolder.Text = PathName;
                listFolderItems.Items.Add("Problem occurred:");
                listFolderItems.Items.Add(e.Message);
            }
            finally
            {
                ListItems = false;
            }
        }

The above code shows the folder names in the specified directory. To show the information of the file we have a small helper function called DisplayFileInfo(...).

C#
protected void DisplayFileInfo(FileInfo TheFile)

  {
       tbFileName.Text = TheFile.Name;
       tbCreationTime.Text = TheFile.CreationTime.ToLongTimeString();
      tbLastAccessTime.Text = TheFile.LastAccessTime.ToLongDateString();
       tbLastWriteTime.Text = TheFile.LastWriteTime.ToLongDateString();
       tbSize.Text = TheFile.Length.ToString() + " Bytes";
    }

In order to delete, move, copy there are much simpler operation available in .NET SDK - like File.Delete, File.Copy and File.Move

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions

 
Questionissue in FileInformation Pin
nirali1310-Jun-13 20:06
nirali1310-Jun-13 20:06 
GeneralMusic Pin
jammmie9998-Jan-09 11:15
professionaljammmie9998-Jan-09 11:15 
GeneralTo get custom file attributes Pin
RodEffect9-Jun-08 20:48
RodEffect9-Jun-08 20:48 
Questiongetting router ip address in c# (Microsoft .net framework 2.0) without the help of other sites.... Pin
Ravis26-Mar-08 19:11
Ravis26-Mar-08 19:11 
QuestionRouter ip address in c# Pin
Ravis25-Mar-08 22:41
Ravis25-Mar-08 22:41 
GeneralRe: Router ip address in c# Pin
leppie25-Mar-08 23:44
leppie25-Mar-08 23:44 
Questionhow can i get all the email addreeses ,url, domain names in a text file Pin
Member 47730964-Jan-08 1:45
Member 47730964-Jan-08 1:45 
QuestionHi,how can i get the total file information Pin
rameshdontagani4-Oct-07 20:16
rameshdontagani4-Oct-07 20:16 
GeneralDetermining if a file is opne/locked Pin
DavidRen2331-May-07 0:59
DavidRen2331-May-07 0:59 
Generalhandy tips - thanks Pin
Anonymous13-May-04 2:25
Anonymous13-May-04 2:25 
GeneralFile Summary Comments Pin
linhpdinh13-Mar-04 16:20
linhpdinh13-Mar-04 16:20 
QuestionHow to determine drive types Pin
Hing17-Nov-03 18:56
Hing17-Nov-03 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.