Click here to Skip to main content
6,594,932 members and growing! (15,366 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Files     Intermediate

File Information using C#

By Samar Aarkotti

Getting information of Files and Directories
C#, VC6, VC7, .NET, Win2K, WinXP, Visual Studio, MFC, Dev
Posted:3 Feb 2003
Views:121,003
Bookmarked:52 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 4.00 Rating: 3.03 out of 5
5 votes, 23.8%
1
1 vote, 4.8%
2
4 votes, 19.0%
3
5 votes, 23.8%
4
6 votes, 28.6%
5

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.

CreationTime Get or sets the creation time of the current FileSystemInfo Object.
Directory get the instance of the parent directory
DirectoryName Get a string representing the directory's full path
Exists Get a value indication whether a file exists
FullName Gets the full path of the directory or file
LastAccessTime Gets or sets the time the current file or directory was last accessed.
LastWriteTime Gets or sets the time when the current file or directory was last written to.
Name Gets the name of the file

Using the code

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

        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.
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(...).

    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

About the Author

Samar Aarkotti


Member
Samar holds a Bachelors degree in computer science from INDIA.

He has been doing programming since 12 years using Java,J2EE,JSP,C++,MFC,VC++,ATL,COM, TCP/IP and now with in C#, He is currently exploring speech technology using C#.
Occupation: Web Developer
Location: United States United States

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralMusic Pinmemberjammmie99912:15 8 Jan '09  
GeneralTo get custom file attributes PinmemberRodEffect21:48 9 Jun '08  
Questiongetting router ip address in c# (Microsoft .net framework 2.0) without the help of other sites.... PinmemberRavis20:11 26 Mar '08  
QuestionRouter ip address in c# PinmemberRavis23:41 25 Mar '08  
GeneralRe: Router ip address in c# Pinmember leppie 0:44 26 Mar '08  
Generalhow can i get all the email addreeses ,url, domain names in a text file PinmemberMember 47730962:45 4 Jan '08  
QuestionHi,how can i get the total file information Pinmemberbeklo.com/rameshgoudd21:16 4 Oct '07  
GeneralDetermining if a file is opne/locked PinmemberDavidRen231:59 31 May '07  
Generalhandy tips - thanks PinsussAnonymous3:25 13 May '04  
GeneralFile Summary Comments Pinsusslinhdinh17:20 13 Mar '04  
GeneralHow to determine drive types Pinmemberliuhoihing19:56 17 Nov '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Feb 2003
Editor: Nishant Sivakumar
Copyright 2003 by Samar Aarkotti
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project