1

I'm trying to set custom culture in my project. But I have some problem I've searched Google and found the following code. But I have some problems with it, please observe it in the comment.

using System;
using System.IO;
using System.Globalization;

public class Example 
{
    public static void Main() 
    {
        // Persist the date and time data.
        StreamWriter sw = new StreamWriter(@".\DateData.dat");

        // Create a DateTime value.      
        DateTime dtIn = DateTime.Now;
        // Retrieve a CultureInfo object.
        CultureInfo invC = CultureInfo.InvariantCulture;

        // Convert the date to a string and write it to a file.
        sw.WriteLine(dtIn.ToString("r", invC));//what r mean?. if r is the custem culture      variabel then how we determin it.
        sw.Close();

        // Restore the date and time data.
        StreamReader sr = new StreamReader(@".\DateData.dat");
        String input;
        while ((input = sr.ReadLine()) != null) 
        {
            Console.WriteLine("Stored data: {0}\n" , input);    

            // Parse the stored string.
            DateTime dtOut = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);

            // Create a French (France) CultureInfo object.
            CultureInfo frFr = new CultureInfo("fr-FR");
            // Displays the date formatted for the "fr-FR" culture.
            Console.WriteLine("Date formatted for the {0} culture: {1}" , 
                       frFr.Name, dtOut.ToString("f", frFr));// f?

            // Creates a German (Germany) CultureInfo object.
            CultureInfo deDe= new CultureInfo("de-De");
            // Displays the date formatted for the "de-DE" culture.
            Console.WriteLine("Date formatted for {0} culture: {1}" , 
                       deDe.Name, dtOut.ToString("f", deDe));
        }
        sr.Close();
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
Irfan Ul Haq
  • 1,065
  • 1
  • 11
  • 19
  • Did you read Microsoft's extensive documentation? – SLaks Oct 10 '13 at 21:24
  • @slaks yes i copy this from there. – Irfan Ul Haq Oct 10 '13 at 21:30
  • Do you get any errors/exceptions, if so, what are they? – Sam Oct 10 '13 at 21:31
  • @sam when i change it to something else like "a" this throw exception stating that "Input string was not in a correct format." – Irfan Ul Haq Oct 10 '13 at 21:34
  • What part of their documentation don't you understand? – SLaks Oct 10 '13 at 21:39
  • @slaks we are the converting the specific(country) Dateime to Global or independent culture(as the Microsoft Documentation says that it is Us English).is it first convert the culture to the "us eng" and then back to what country format we are trying to? if so how i could determine the stander and culture specific formats and symbols-- like there is f in the above code both form German and French format and r for something else format-- so i have no clue about the formats part. – Irfan Ul Haq Oct 11 '13 at 08:49
  • 1
    @Irfan: The format specifiers are the same across cultures; each one will use an appropriate format for that culture. – SLaks Oct 11 '13 at 12:56
  • @Slaks please can you guide me that how i can determine different format specifiers for different culture. or direct me to some more sophisticated knowledge on cultures. – Irfan Ul Haq Oct 11 '13 at 19:11
  • http://msdn.microsoft.com/en-us/library/az4se3k1.aspx – SLaks Oct 11 '13 at 19:17
  • @Slaks thnx it sound like helping altot. – Irfan Ul Haq Oct 12 '13 at 16:33

1 Answers1

1

Here's a link that shows many formatting values for the DateTime.ToString() method. I see no lower case "r" mentioned but the output of your code seems be the same with "R" or "r".

http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

The DateTime value that you are writing to the file would be based on the invariant culture before any culture changes. You write it out and the you read it back in before getting some new culture information.

I had to guess at what you were asking because there is no question anywhere but in the code. Please provide more detail if I misunderstood what you are asking about.

Maybe if you were to show your output, it would help.

Ah, and here's a link that actually says that "r" is the same as "R". So now you have documentation for that part of your question:

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

David Rector
  • 958
  • 9
  • 31