Retrieves the date from the beginning of the WinSat filename and creates a datetime object from it : File Name « File Stream « C# / C Sharp






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;
    }

  }
}

   
    
    
    
    
  








Related examples in the same category

1.Query file name with Linq
2.Clean File Name
3.Get File Name
4.Convert To Legal File Name
5.Get File Name From Path
6.Extracts a path part of a full path filename, searching for the last directory separator or column.
7.Creates a relative path from absolute pathfilename and a reference path.
8.Get file extension
9.Restrict a list of files to valid extensions only
10.Get Content Type by Extension
11.Get Full Path Without Extension
12.Full Name Without Extension
13.Name Without Extension
14.ImageFormat to Extension
15.Gets the extension without the dot
16.Get Image Type Extension