Mega Code Archive

 
Categories / C# / File Stream
 

Retrieves the date from the beginning of the WinSat filename and creates a datetime object from it

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace WEI_Share.Helpers {   public sealed class Utilities   {     /// <summary>     /// Retrieves the date from the beginning of the WinSat filename and creates a datetime object from it     /// </summary>     /// <param name="fileName"></param>     /// <returns>The converted datetime or null</returns>     public static DateTime? GetDateFromFileName(string fileName)     {       //VISTA:  2009-06-23 17.13.21.155 Assessment (Formal).WinSAT.xml       //int position = fileName.IndexOf("Assessment");       //Win7:  2009-06-23 17.13.21.155 Formal.Assessment (Recent).WinSAT.xml       int position = fileName.IndexOf("Formal.Assessment");       if (position > -1)       {         if (fileName.Length >= (position - 1))         {           string dateTimeText = fileName.Substring(0, position - 1);           position = dateTimeText.LastIndexOf('.');           if (position > -1)           {             return Convert.ToDateTime(dateTimeText.Substring(0, position - 1).Replace(".", ":") + dateTimeText.Substring(position));           }         }       }       return null;     }   } }