views:

71

answers:

2

I have the following user setting defined in app.config:

<userSettings>
    <MyProject.Properties.Settings>
        <setting name="LastProcessedDate" serializeAs="String">
            <value>07/06/2010 13:05:10</value>
        </setting>
    </MyProject.Properties.Settings>
</userSettings>

Is there a way to specify that this setting should be serialized with the milliseconds - e.g. 07/06/2010 13:05:10.181 - so that I can accurately compare it to a SQL Server datetime field?

+5  A: 

Unfortunately, you can't save millisecond values in settings. Deep down in the System.Configuration.SettingsPropertyValue.ConvertObjectToString method the DateTime value is converted to a string using the TypeConverter.ConvertToInvariantString method which doesn't produce milliseconds.

If you really want that level of accuracy and you must save it in the user settings, you should use a different parameter type like string using a custom format that includes milliseconds. None of the standard time formats includes milliseconds.

Panagiotis Kanavos
Thanks for a very simple solution. I used a setting of type string and called DateTime.Parse when reading the setting and ToString('yyyy-MM-dd HH:mm:ss.fff') when writing it. Works like a charm!
MCS
+1  A: 

You could try storing it as an Int64, which would preserve the entire value of the DateTime without any formatting issues or loss of fidelity.

codekaizen