I want to try to (programmatically) guess the date a file was created. It it is named BlaBla.2012-06-04.xlw, I can guess it was created June 4th, 2012.
However, if the file is named something else, like SitOnA.PotatoPanOtis.pal, I want to be able to simply give up (without the attempted conversion to a DateTime causing an exception) and present the user with a DateTimePicker.
Doable?
I’ve got this code:
String substr = SelectedFileName.Substring(DATE_BEGIN_POS, DATE_LENGTH);
return DateTime.Parse(substr);
I’m thinking something like this would make sense:
try
{
String substr = SelectedFileName.Substring(DATE_BEGIN_POS, DATE_LENGTH);
return DateTime.Parse(substr);
}
catch (ConversionException ce)
{
//Show form with DateTimePicker
}
UPDATE
It seems like something like this should work:
DateTime dt;
if (!(DateTime.TryParse(substr, out dt))) {
. . .
Either you can ask directly for the creation time using the
File.GetCreationTimeor if you are trying to guess via the file name then you can use TryParse for the date and if doesn’t work (method return false) then show the dialog.