Reputation: 309
foreach (var file in Directory.GetFiles(@"C:\Users\Andrew\Desktop\Timesheets\2011", "*.xlsx", SearchOption.AllDirectories)){
Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf("_" + 1), 6), "dd/mm/yy", null), file);
_Paychecks.Add(p);
}
I am trying to get a DateTime
from a filename that my program will be scanning through. The DateTime
is formatted as dd/mm/yy. Every time I get to this line of code:
Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(file.LastIndexOf("_" + 1), 6), "dd/mm/yy", null), file);
The program breaks to the application without doing anything. It will not give me an error message and any information as to what is going on. I'm sure I've written this in some sort of confusing manner so let me know what you need clarification on.
Here is an example of one of the file names incase that helps;
Weekly Time & Expense Sheet_AT_073111
Upvotes: 1
Views: 146
Reputation: 524
Try this.
while converting string to DateTime, use
Convert.ToDateTime(//your string which is converted to DateTime//).
Upvotes: 0
Reputation: 26209
You need to use capital MM
to represent Month
instead of small mm
from your sample Date string 07312011
it looks like
first two digits - 07
=> Month
next two digits - 31
=> Date
next four digits - 2011
=> Year
hence your format should be MMddyyyy
EDIT: you need to add 1 after closing the LastIndexOf()
function
Try This:
Paycheck p = new Paycheck(DateTime.ParseExact(file.Substring(
file.LastIndexOf("_") + 1, 6), "MMddyyyy", null), file);
Upvotes: 4