?
Solved

My.Settings Won't actually save

Posted on 2012-09-01
9
Medium Priority
?
706 Views
Last Modified: 2012-09-03
Hello Experts,

I'm putting together a small application that uses a few user settings in my.settings.  I am saving the changes with my.settings.save() and as long as the applicaiton is open and stays running the settings look to be changed in the properties.  WHen I close the app and reopen it the settings are back to their original values.  I'm guessing the app is saving to the wrong file.  I started programming it on a desktop then took it on the road by copying the folder to my laptop.  Is there a place where I need to tell VS 2010 where to save the config file?  The App.Config file in the project folder looks to be the one NOT updating properly.

THanks for your thoughts
0
Comment
Question by:zipnotic
  • 5
  • 2
  • 2
9 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 38357448
Can you show some code?
0
 

Author Comment

by:zipnotic
ID: 38357485
Getting and saving:    

       If ds.Tables("Events").Rows.Count > 0 Then
                My.Settings.EventPK = ds.Tables("Events").Compute("Max(Event_PK)", Nothing)
                Me.txtLastEventPK.Text = My.Settings.EventPK
                My.Settings.DateUpdated = Now()
                Me.txtLastDate.Text = Now().ToString
                LogIt("Updated MAX PK to " & Me.txtLastEventPK.Text)
            End If
            My.Settings.Save()

Using:

sql = "SELECT * FROM [v_Event] WHERE [Event_PK] > '" & My.Settings.EventPK & "'"
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 38357491
Are you getting the right results in Me.txtLastEventPK.Text and Me.txtLastDate.Text ?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:zipnotic
ID: 38357501
Yes, as long as the program stays open.  AT each update it is the correct Number.  After I close and reopen the EventPK reverts to the original setting when it should stay at the last/higher number.  Ultimately I am trying to track only the new records added to the table.
0
 

Author Comment

by:zipnotic
ID: 38357511
I believe I have solved the issue.  Under PROPERTIES for the SETTINGS.VB file I changed to COPY ALWAYS instead of do not copy.  This forces the compiler to add and update the Settings when debugging.  Unfortunately it does NOT update the original settings file...Any idea on how to update both as I develop the program?

AND ensure that the settings get updated when deployed to a different computer?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 38357523
First of all, I would put a breakpoint on My.Settings.Save to make sure that it is hit. How many times do all of us make that mistake or assuming that a line of code execute when it does not :-).

Then, app.config will never change, unless you do it manually. Consider it as source code.

When you build the application (bin directories by default), app.config is copied in the compilation directory and renamed NameOfApplication.exe.config. This file must be installed in the same directory as the application.

This file will never be updated by Save however. It contains the application settings, that are read-only, and the default values for the users settings.

The first time a user runs the application, those defaults values are copied as user.config in the users data directory, in a directory whose name changes depending on a few factors. Here is the path in Windows 7 for a user named Jacques, and application created by the company JBFI (as specified in AssemblyInfo), and an application called JBMusic, whose AssemblyVersion is 0.6.0.0. This is the vshost version of the application, meaning that it is the one that runs inside of the Visual Studio IDE. For a regular user, the file would be JBMusic.exe_somethingElse.

C:\Users\Jacques\AppData\Local\JBFI\JBMusic.vshost.exe_Url_nbg2a3104ca0m1ldr5k4p05omffeaw2r\0.6.0.0\user.config

This is the file that is updated when you save the settings, not app.config. And it is a different file for each user on the computer.

Another reason that may cause the settings not to reload is if you change the AssemblyVersion, because that version number is used for the configuration directory. For that reason you should change that version number only when really necessary, usually when you deploy the application to the users. Change the AssemblyFileVersion instead if you want to follow up on your builds.

When the AssemblyVersion is changed, you need to call My.Settings.Upgrade to copy the previous settings to the new version.
0
 

Author Comment

by:zipnotic
ID: 38357541
Is there anyway to use a universal 'my.settings' not dependent on the the user logged on, or am I stuck managing my own XML file if I want to go that route?
0
 
LVL 40
ID: 38357602
The standard My.Settings has been created to help in the most common scenario.

If you want to do otherwise, you have to either implement your own system, or inherits from System.Configuration.ApplicationSettingsBase to modify the way the default configuration system is working.
0
 

Author Closing Comment

by:zipnotic
ID: 38360546
Who could have known?  Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Integration Management Part 2
Loops Section Overview
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question