A convenient wrapper class to get file info

If you have worked with file information in .NET, you have surely run into the fact that SOME of the information is gotten from the FILE class and some from the FILEINFO class, which can get a little confusing. To simplify access to file information, I created this wrapper class called FileStats: the code shows you how to create a new object by sending a full path and name (e.g. "C:\try\test.txt") and then you have an object with parameters that give you the information you need most about files: PathAndName, Name, SizeInBytes, Extension, CreationDateTime, and LastModifiedDateTime. If the file does not exist, then an FileNotFoundException is thrown. This code sample shows how to handle it as well. Enjoy.

<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>

<script runat="server">

   public void Page_Load(Object sender, EventArgs e) {
   
       string[] pathFiles = new string[4];
       pathFiles[0] = @"C:\try\objects.csx";
       pathFiles[1] = @"C:\try\objects.cs";
       pathFiles[2] = @"C:\Inetpub\wwwroot\TestForms\Default.aspx";
       pathFiles[3] = @"alksjdflöaskjflaskdfj";
       
       for(int x=0 ; x < pathFiles.Length ; x++) {          
           try {
               FileStats fs = new FileStats(pathFiles[x]);
               Response.Write(fs.ToString());
           }
       
           catch(FileNotFoundException ex) {
               Response.Write("File does not exist: <b>" + pathFiles[x] + "</b><br>");
           }
           Response.Write("<br>");
       }
   }
   
   
   private class FileStats {
   
       private string pathAndName;
       private string name;
       private long sizeInBytes;
       private string extension;
       private DateTime creationDateTime;
       private DateTime lastModifiedDateTime;
   
       public string PathAndName {
           get { return pathAndName; }
           set { pathAndName = value; }
       }
   
       public string Name {
           get { return name; }
           set { name = value; }
       }
   
       public long SizeInBytes {
           get { return sizeInBytes; }
           set { sizeInBytes = value; }
       }
   
       public string Extension {
           get { return extension; }
           set { extension = value; }
       }
   
       public DateTime CreationDateTime {
           get { return creationDateTime; }
           set { creationDateTime = value; }
       }
   
       public DateTime LastModifiedDateTime {
           get { return lastModifiedDateTime; }
           set { lastModifiedDateTime = value; }
       }
   
       //constructor: awaits "c:\try\test.txt" for example
       public FileStats(string pathAndName) {
          this.pathAndName = pathAndName;
          if (File.Exists(pathAndName)) {
   
               //use FILEINFO to get the name, length and extension of the file
               FileInfo  fi = new FileInfo(pathAndName);
               this.name = fi.Name;
               this.sizeInBytes = fi.Length;
               //build extension
               int PositionOfDot = fi.Name.IndexOf(".");
               int LengthOfFileName = fi.Name.Length;
               this.extension = fi.Name.Substring(PositionOfDot+1,(LengthOfFileName-PositionOfDot)-1);
   
               //use FILE to get the date information about the file
               this.CreationDateTime = File.GetCreationTime(pathAndName);
               this.LastModifiedDateTime = File.GetLastWriteTime(pathAndName);
               
          } else {
               throw new FileNotFoundException();
          }
       }
   
       public override string ToString() {
           StringBuilder sb = new StringBuilder();
           sb.Append("<table border=\"1\" cellpadding=\"5\">");
           sb.Append("<tr><td>Full path and file name: </td><td><b>" + this.PathAndName + "</b></td></tr>");
           sb.Append("<tr><td>File name: </td><td><b>" + this.Name + "</b></td></tr>");
           sb.Append("<tr><td>Extension: </td><td><b>" + this.Extension + "</b></td></tr>");
           sb.Append("<tr><td>Size in bytes: </td><td><b>" + this.SizeInBytes.ToString() + "</b></td></tr>");
           sb.Append("<tr><td>Created date: </td><td><b>" + this.CreationDateTime.ToString() + "</b></td></tr>");
           sb.Append("<tr><td>Last modified date: </td><td><b>" + this.LastModifiedDateTime.ToString() + "</b></td></tr>");
           sb.Append("</table>");
           return sb.ToString();
       }
   }

</script>

You might also like...

Comments

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”