none
Where is my application getting these setting values from?

    Question

  • So I'm working on a project that will control the blinds in my house, it should be a fairly simple application, but I've run into a snag with the My.Settings.

    Under the settings tab of my project, I defined 3 strings for my timers. 3 Strings called OpenTime, CloseTime, and RefrshTime. They are defined as "6:00 AM", "6:00 PM", "4:00 AM" (respectively) in the settings tab of the project properties. When I run my application in debug, in my Form1 load routine I have these lines:

    Try
      refreshTime = DateTime.Parse(My.Settings.RefreshTime)
      closeTime = DateTime.Parse(My.Settings.CloseTime)
      openTime = DateTime.Parse(My.Settings.OpenTime)
    Catch ex As Exception
      stsBar.Text = "Failed to parse set times from Settings."
    End Try

    If I throw a break point on any of these lines, and get the value of My.Settings.RefresTime,My.Settings.CloseTime, or My.Settings.OpenTime it is "2:00 AM" (they are all the same incorrect value). If I then change the values, and save my settings, it does update the user.config file like so:

                <setting name="OpenTime" serializeAs="String">
                    <value>7:00 AM</value>
                </setting>
                <setting name="CloseTime" serializeAs="String">
                    <value>9:00 AM</value>
                </setting>
                <setting name="RefreshTime" serializeAs="String">
                    <value>8:00 AM</value>
                </setting>

    However, when I close my application and restart it, it loads "2:00 AM" into all the strings again.

    These vales are not in the user.config file, they are not the default values in the application.exe.config file. If I run the program outside of the debugger, the same issue occurs. All other settings work properly, it is only these 3 Strings that are causing problems. I will also note that the "RefreshTime" setting WAS set to "2:00 AM" at one time, but the other Strings were never set to "2:00 AM". Any thoughts on where else it may be getting these rogue strings from?

    -Thanks



    • Edited by Shadow42 Tuesday, February 12, 2019 4:00 AM
    Tuesday, February 12, 2019 3:58 AM

Answers

  • The issue didn't occur with the sample code you provided, which loads the settings basically the same way my code does so I started digging into code other then my Load method.

    I was thinking the "Form1_Load" Method would be the first method called when my application executes, but as it turns out, it calls the "valueChanged" methods for all the DateTimePicker controls on the Form first even though the form hasn't loaded yet. I caught this because I noticed the default value for all the controls was 2:00 AM. now I need to look into how to suppress writing to my settings values before the form is actually loaded.

    Thanks for the help.

    • Marked as answer by Shadow42 Wednesday, February 13, 2019 2:30 AM
    Wednesday, February 13, 2019 2:30 AM

All replies

  • Hi,

    Maybe you are not modifying the correct config file.You can open the <AssemblyName>.exe.config file associated with your application using Microsoft Notepad or some other text or XML editor. (The file is in the Debug or Release folder under the Bin folder).

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 12, 2019 6:07 AM
    Moderator
  • Hello,

    Add the following to your project.

    https://github.com/karenpayneoregon/MySettingsVisualBasic/blob/master/Basic/MySettingsAdditions.vb

    Then call 

    If My.Settings.ConfigurationFolderExists Then
        MessageBox.Show(My.Settings.ConfigurationFolder)
    End If
    Does it get the folder you expected?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 12, 2019 11:36 AM
    Moderator
  • The <AssemblyName>.exe.config file contains the original values that were defined in the Settings tab of the project properties:

                <setting name="OpenTime" serializeAs="String">
                    <value>6:00 AM</value>
                </setting>
                <setting name="CloseTime" serializeAs="String">
                    <value>6:00 PM</value>
                </setting>
                <setting name="RefreshTime" serializeAs="String">
                    <value>4:00 AM</value>
                </setting>

    And I am not updating the user.config file in question, my application is updating it when I issue the My.Settings.save() command. I figured this was the correct user.config, since it does contain all the settings I've changed with values I expect to see.

    -Thanks

    Tuesday, February 12, 2019 12:45 PM
  • I will give this a try when I get home from work.

    -Thank you

    Tuesday, February 12, 2019 12:49 PM
  • The messagebox displays the path of the user.config file I mentioned in my first post, which is what I expected. Any thoughts on why it is getting the wrong values even though it is looking in the correct file?

    Edit: so I added "My.Settings.Reload" as the first command in my code and it fixes the issue. Does the program not automatically load your settings when it starts?

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'If My.Settings.ConfigurationFolderExists Then
            '    MessageBox.Show(My.Settings.ConfigurationFolder)
            'End If
            My.Settings.Reload()
            Try
                refreshTime = DateTime.Parse(My.Settings.RefreshTime)
                closeTime = DateTime.Parse(My.Settings.CloseTime)
                openTime = DateTime.Parse(My.Settings.OpenTime)
            Catch ex As Exception
                stsBar.Text = "Failed to parse set times from Settings."
            End Try
    End Sub



    • Edited by Shadow42 Tuesday, February 12, 2019 11:22 PM
    • Marked as answer by Shadow42 Tuesday, February 12, 2019 11:23 PM
    • Unmarked as answer by Shadow42 Tuesday, February 12, 2019 11:24 PM
    Tuesday, February 12, 2019 11:12 PM
  • No, Reload does not do what it should always and have steered clear of this method.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 12, 2019 11:54 PM
    Moderator
  • Reload seems to be working, if I remove the reload call, the settings are again incorrect. Is there a better method to properly load my application settings? 
    Wednesday, February 13, 2019 12:57 AM
  • Reload seems to be working, if I remove the reload call, the settings are again incorrect. Is there a better method to properly load my application settings? 

    Try out this code sample I wrote.

    https://1drv.ms/u/s!AtGAgKKpqdWjjQmjBRY02cRl67rc


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 13, 2019 1:14 AM
    Moderator
  • The issue didn't occur with the sample code you provided, which loads the settings basically the same way my code does so I started digging into code other then my Load method.

    I was thinking the "Form1_Load" Method would be the first method called when my application executes, but as it turns out, it calls the "valueChanged" methods for all the DateTimePicker controls on the Form first even though the form hasn't loaded yet. I caught this because I noticed the default value for all the controls was 2:00 AM. now I need to look into how to suppress writing to my settings values before the form is actually loaded.

    Thanks for the help.

    • Marked as answer by Shadow42 Wednesday, February 13, 2019 2:30 AM
    Wednesday, February 13, 2019 2:30 AM
  • The issue didn't occur with the sample code you provided, which loads the settings basically the same way my code does so I started digging into code other then my Load method.

    I was thinking the "Form1_Load" Method would be the first method called when my application executes, but as it turns out, it calls the "valueChanged" methods for all the DateTimePicker controls on the Form first even though the form hasn't loaded yet. I caught this because I noticed the default value for all the controls was 2:00 AM. now I need to look into how to suppress writing to my settings values before the form is actually loaded.

    Thanks for the help.

    If you have

    Private Sub DateTimePicker1_ValueChanged(sender as Object, e as EventArgs) _ 
         Handles DateTimePicker1.ValueChanged
    Then remove the Handles clause and then in the last line of Form Shown use AddHandler to setup this event so it does not fire until an actual value changes by the user.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 13, 2019 11:38 AM
    Moderator