Reputation: 2085
I have come across a strange problem. MY app has been localized to support 5 languages.
It is a calendar sort of app, and i have an option of writing notes for a particular date.
So when I change my language settings to Russian, enter a note on a date, eg 1/3/2012 and on 6/3/2012. This date and note is stored in an IsolatedStorageFile
and i exit the app.
Now, I change my language settings to english, and the notes are stored in 3/1/2012 and 3/6/2012. The month and date are reversed !! :(
This is how i write the notes to the file
using (IsolatedStorageFileStream fileStream = storagefile.OpenFile("NotesFile", FileMode.Open, FileAccess.ReadWrite))
{
StreamWriter writer = new StreamWriter(fileStream);
for (int i = 0; i < m_noteCount; i++)
{
writer.Write(m_arrNoteDate[i].ToShortDateString());
writer.Write(" ");
writer.Write(m_arrNoteString[i]);
writer.WriteLine("~`");
}
writer.Close();
}
Reading is from file is done like this
if (notes.Substring(i, 1) == " ")
{
m_arrNoteString[count] = notes.Substring(i + 1);
string temp = notes.Substring(0, i);
m_arrNoteDate[count] = DateTime.Parse(temp);
count++;
nextCharacter = (char)reader.Read();
notes = "";
break;
}
the parser reads date in a format according to language the device is set to? Any work arounds?
Alfah
Upvotes: 0
Views: 309
Reputation: 15400
By default, the conversion between dates and strings (and vice versa) uses the current culture. For your purpose, since you are not converting to/from strings for the purpose of displaying dates to the user but to persist them, you should make your logic independent of the current culture by specifying a given culture. And the invariant culture is your best best.
So replace this:
writer.Write(m_arrNoteDate[i].ToShortDateString());
With this:
writer.Write(m_arrNoteDate[i].ToString("d", CultureInfo.InvariantCulture);
And replace this:
m_arrNoteDate[count] = DateTime.Parse(temp);
With this:
m_arrNoteDate[count] = DateTime.Parse(temp, CultureInfo.InvariantCulture);
Upvotes: 1
Reputation: 700
Use m_arrNoteDate[i].ToString("d", System.Globalization.CultureInfo.InvariantCulture) when writing date to have it independent of the thread culture.
Upvotes: 1
Reputation: 8126
DateTime.Parse
has an overloaded method where you can pass CultureInfo.InvariantCulture
to read it independently of region settings. Also, you need to save value accordingly too.
Also, look at Thread.CurrentThread.CurrentCulture
and Thread.CurrentThread.CurrentUICulture
. You can set specific culture to application globally.
Upvotes: 1