question

SharpWindows avatar image
0 Votes"
SharpWindows asked DanielZhang-MSFT commented

Properties.Settings.Default does not work

Hello everybody.
In a Windows Forms program i am trying to save a string. Well, the method does not save it.
Please help.

what i've written:
string abc = "abcdefghijklmnopqrs";
Properties.Settings.Default.a_value = abc;
Properties.Settings.Default.Save();
//a_value is a string

Thanks in advance.
bye

dotnet-csharpwindows-forms
· 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.


How did you discover it?


0 Votes 0 ·

Hello viorel-1.
the next time you run the program the value is not updated

thanks

0 Votes 0 ·

Hello @DanielZhang-MSFT
Sorry, i haven't explained the issue correctly.
UToken is a random string. The random string generator is UToken_gen()

  Properties.Settings.Default.UToken = UToken_gen();
                             Properties.Settings.Default.Save();

when i run the program again:

string UToken_after_restarting = Properties.settings.Default.UToken;

i've found like there's 1:3 possibility when you restart the program, UToken_after_restarting is the same as the UToken saved. Most of all times UToken_after_restarting is another UToken (i mean new generated from UToken_gen() ) I can't understand where is the problem. Please Help. Thanks again. Bye

0 Votes 0 ·

Hi @VBnetcoder-0336,
Could you provide the codes about your "UToken_gen()" to reproduce the situation?
Best Regards,
Daniel Zhang

0 Votes 0 ·

1 Answer

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

Hi VBnetcoder-0336,
>>the next time you run the program the value is not updated.
When I run it again, the value I get is "abcdefghijklmnopqrs". I guess what you mean is that the value in the textBox of the value corresponding to "a_value" in the Setting page has not been updated.
As this document said that Settings that are application-scoped are read-only, and can only be changed at design time or by altering the .config file in between application sessions. Settings that are user-scoped, however, can be written at run time just as you would change any property value.
The new value persists for the duration of the application session. You can persist the changes to the settings between application sessions by calling the Save method.
Since design-time tools are not available after the application has been compiled and deployed, you must change the setting value manually in the file.
Locate the entry for the setting you want to change. It should look similar to the following example(App.config):

 <setting name="a_value" serializeAs="String">
                 <value>test</value>
  </setting>

More details you can refer to this document.
If I misunderstood what you mean, please explain in detail.
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.


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.