try this
it will work for "
MMddyyyy
" and "
Mddyyyy
"
string filename = "Accounts_2012017.txt";
string temp = filename.Replace(".txt", "").Substring(filename.LastIndexOf('_') + 1);
string format = (temp.Length == 7) ? "Mddyyyy" : "MMddyyyy";
DateTime extracted = DateTime.ParseExact(temp, format, System.Globalization.CultureInfo.InvariantCulture);