Put Date in App.Config

Asked

Viewed 194 times

2

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

What I want is something like:

<add key="Data" value="DateTime.Now" />
  • The Configurationmanager class is a good starting point. http://msdn.microsoft.com/en-us/library/System.Configuration.ConfigurationManager%28v=vs.110%29.aspx

  • I updated the question with what I want. In this case, this information will not be very useful, because I already know that. Thanks for the tip, but in this case it won’t be what I need

  • You want to parse the value?

  • I found the answer you wanted. &#xD; [http://stackoverflow.com/a/21778933/3206532][1]&#xD; &#xD; [1]: http://stackoverflow.com/a/21778933/3206532

1 answer

3


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

If you want to set the default value to DateTime.Now, or allow it to be another value, you 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 him 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 --> 

It may be necessary to exchange Parse for ParseExact to have more control over date formats.

  • 3

    Of course there is a good solution to this problem in English. Look at her there!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.