question

KonstandDuPlessis-5459 avatar image
0 Votes"
KonstandDuPlessis-5459 asked ErickHeredia-5060 answered

ClickOnce overwrite user settings (.net core 3.1)- Settings.Default.Upgrade() not working

Hi all

I have a small offline wpf app making use of user settings and click once deployment.
No matter what I do, I cannot retain the user settings after clickonce updates and resets all values to defaults.
I have tried the Settings.Default.Upgrade() recommendation, but it does not work.
Settings.Default.GetPreviousVersion("propertyname") also always returns null.

I also tested this in a small desktop app, same results. is this something missing in .net core 3.1?

I am left to either throw out the use of built in user settings or click once...

windows-wpf
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KonstandDuPlessis-5459
How did you save for the user settings or configurations? As described in this answer. Is there any thing special for your ClickOnce? and Could you show the detailed steps to reproduce your error?


0 Votes 0 ·

Hi @DaisyTian-MSFT. To reproduce:
1. Create new WPF or WinForms app using .net Core 3.1
2. Go to project properties, Settings tab and add:
TestSetting/string/User/1
as well as
updateSettings/bool/User/True
3. on main window add a button with click event:
Properties.Settings.Default.TestSetting = "2";
Properties.Settings.Default.Save();
You can also add a label and assign it's text to the property TestSetting or use debug to evaluate the property value
4. add to startup or window loaded event
if (Properties.Settings.Default.updateSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.updateSettings = false;
Properties.Settings.Default.Save();
}
5. Build+publish using the default settings of clickonce (updates to a UNC path that is a share on your pc)
6. Install and run app, click button to set property to 2
7. publish ,open and update app. property will be 1 again.


0 Votes 0 ·

you can close and open app again after step 6 to ensure the value of 2 was saved

0 Votes 0 ·

within the method that does the upgrade, you can also evaluate in a quickwatch the value
Properties.Settings.Default.GetPreviousVersion("TestSetting") - it will be null

0 Votes 0 ·

Hi,

Did you ever find a solution? I'm facing the same problem and have tried all solutions posted here and in linked posts, but to no avail.

Thanks.

0 Votes 0 ·

Hi,

Did you ever find a solution? I'm facing the same problem and have tried all solutions posted here and in linked posts, but to no avail.

Thanks.

0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

Hi KonstandDuPlessis-5459,
If a previous version of the settings file exists, it will copy its value to the latest version every time the application is launched.
In other words,ClickOnce copy user-level settings forward to the next version's cache when the user gets an update.
For more discussion on this problem, you can refer to this thread.
And you can try the method that Agustin Meriles provided.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DanielZhang-MSFT
Thanks for your reply. Unfortunately the solution you provided does not work in .net core 3.1
Please see my reply to @DaisyTian-MSFT above and try to recreate the issue with the steps I mentioned. Hopefully you see the same results as me

0 Votes 0 ·

Hi KonstandDuPlessis-5459,
I followed your description step by step and my label also shows "2".
Here are some of what I did:

 private void button1_Click(object sender, EventArgs e)
 {
     Properties.Settings.Default["TestSetting"] = "2";    
     Properties.Settings.Default.Save();
     label1.Text = Properties.Settings.Default.TestSetting;
 }
    
 private void Form1_Load(object sender, EventArgs e)
 {
     if (Properties.Settings.Default.updateSettings)
     { 
         Properties.Settings.Default.Upgrade();
         Properties.Settings.Default.updateSettings = false;
         Properties.Settings.Default.Save();
     }
 }

62300-21.png
So I install and run app, click button to set property to 2. And then publish it again.
The labe also shows "2".
What am I missing or is there a misunderstanding? please explain in detail.
Best Regards,
Daniel Zhang


0 Votes 0 ·
21.png (13.6 KiB)

Thanks for your reply Daniel. then I am at a loss... That is exactly what I do as well, but it does not work. I don't get any errors, but after the "if" in the form1_load was executed, mine is still on 1.
on what target framework is your project?

One thing, my project is a WPF project and not winforms. So I call the upgrade code in the _Loaded event. might that be the wrong place?
I also tried it in the override startup method in App.Xaml.cs, but still not working.

I did however do a work around in the meantime by creating a custom config file for the app on startup in the appdata/local/appname folder. since it is created and then maintained from code, ClickOnce does not deploy it and do not overwrite it on updates. that worked 100%, but still wondering why the original code did not work.

0 Votes 0 ·
Show more comments
KonstandDuPlessis-5459 avatar image
0 Votes"
KonstandDuPlessis-5459 answered DanielZhang-MSFT commented

There is nothing strange to the code, but it still overwrites the 2 with 1 after update



MainForm.xaml.cs:

using System.Windows;

namespace TestSettingsApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

     private void Button_Click(object sender, RoutedEventArgs e)
     {
         Properties.Settings.Default.Setting = settingTxt.Text;
         Properties.Settings.Default.Save();
         settingLbl.Content = Properties.Settings.Default.Setting;
     }

     private void Grid_Loaded(object sender, RoutedEventArgs e)
     {            
         if (Properties.Settings.Default.UpgradeSettings)
         {
             Properties.Settings.Default.Upgrade();
             Properties.Settings.Default.UpgradeSettings = false;
             Properties.Settings.Default.Save();
         }
         settingLbl.Content = Properties.Settings.Default.Setting;
     }
 }

}

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @KonstandDuPlessis-5459,
I made a test by using WPF, and I also got the same result with winform.
Here is my test project.
To reproduce the situation, please share your whole project whitout private information.
And if it reached the size limitation, please share the project using OneDrive or Github.
Best Regards,
Daniel Zhang


0 Votes 0 ·
ErickHeredia-5060 avatar image
0 Votes"
ErickHeredia-5060 answered

In case someone hasn't found a way to fix this.

What worked for me to solve it was Sign the application in Project properties -> Singing -> "Sign the assembly"

this even though I already sign the application when publishing in click once, then I sign the application in both places.

201835-image.png



image.png (32.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.