views:

195

answers:

2

I'm wanting to put a DateTime in to the config file, however, I want the DateTime expressed in a specific way. I've seen examples of using a DateTime in a ConfigurationElement (like the example below). The examples I've seen all have the date expressed in American format. I want to ensure that date is understandable by all regardless of who they are so I want to use yyyy-MM-dd HH:mm:ss as the format.

How do I do that when using a class derived from ConfigurationElement?

    class MyConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("Time", IsRequired=true)]
        public DateTime Time
        {
            get
            {
                return (DateTime)this["Time"];
            }
            set
            {
                this["Time"] = value;
            }
        }
    }
+2  A: 

Are you sure? Afaik, the default is XML style and that is what you want too (yyyy-mm-dd).

Henk Holterman
Doh! Yes, it works without modification. That's what I get for trying to read up on stuff and not actually trying it out to see for myself. Dr. Feynman will be spinning in his grave.
Big Hair
+1  A: 

I guess you can use the following:

[ConfigurationProperty("Time", IsRequired=true)]
public DateTime Time
{
    get
    {
        return DateTime.ParseExact(
            this["Time"].ToString(),
            "yyyy-MM-dd HH:mm:ss",
            CultureInfo.InvariantCulture);
    }
    set
    {
        this["Time"] = value.ToString("yyyy-MM-dd HH:mm:ss");
    }
}
M4N