Place Date in App.Config

2

I have an application that runs as a service and needed to configure some variables, such as dates, in the app.config so that when it is necessary to make changes to the search criteria (in cases where a failure may occur) in debug the application.

What I want is something like:

<add key="Data" value="DateTime.Now" />
    
asked by anonymous 14.02.2014 / 12:24

1 answer

3

This was the answer that helped me, from Stack Overflow , translated into Portuguese:

If you want to set the default value to DateTime.Now , or allow it to be another value, you'll probably want code similar to this:

Dim magicDate = If(Not String.IsNullOrWhiteSpace(
                        ConfigurationManager.AppSettings("OverrideMagicDate")),
          Date.Parse(ConfigurationManager.AppSettings("OverrideMagicDate")),
          Date.Now) 

In the file app.settings , leave the value of OverrideMagicDate empty if you want it to receive DateTime.Now or enter the date you want:

<add key="OverrideMagicDate" value="" /><!-- DateTime.Now -->

or:

<add key="OverrideMagicDate" value="2012-01-13" /><!-- 13/01/2012 --> 

You may need to swap Parse by ParseExact if you want have greater control over date formats.

    
14.02.2014 / 13:43