This is the first time in this forum, and my question is:
This code fragment is very simple
string s = DateTime.Now.ToString(Thread.CurrentThread.CurrentCulture);DateTime.Parse(s, Thread.CurrentThread.CurrentCulture);
OR
DateTime.Parse("25/01/05 11.27.31", CultureInfo.CreateSpecificCulture("it-IT"));OR
DateTime.Parse("dummy", CultureInfo.CreateSpecificCulture("it-IT"));
I expected a FormatException if the date is wrong, but in all cases the result is a IndexOutOfRangeException.
Where is the problem
The configuration of my PC is:
- Windows XP Professional Service Pack 2 (Italian);
- Visual Studio 2005 v. 8.0.50727.42 (Italian);
- .NET Framewok v. 2.0.50727 (Italian).
With .NET Framework 1.1 and Visual Studio 2003 this problem does not exist.
Thanks to all.

DateTime.Parse problem (IndexOutOfRange Exception)
version2
Thanks for your time, I have tried with .NET Framework (EN) but the result is the same.
After many trying, i don't know why, but the code work in a console application.
The problem is that I use this code in a thread launch from Windows Service, in this case the problem is present.
Suggestions
Thanks
-:(
JohnD90
I have resolved the problem in this way:
string[] allPatterns = Thread.CurrentThread.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns();foreach (string sPattern in allPatterns) {
try {
return DateTime.ParseExact(sLastStart, sPattern, Thread.CurrentThread.CurrentCulture);
} catch { }
}
Is very strange, but in the list is present a pattern that work and the ParseExact() return a DataTime.
I don't know why
JockForrester
Chompin
Hi, i've proved that lines of source code in a windows service and i have no errors. It works perfectly.
See ya.
CyK
System.FormatException was unhandled
Message="The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.".
I gues is something wrong with the Italian version, try to download C# Express En edition and test it there.