C# Properties.Settings Property doesn't setter any value
Asked Answered
R

3

6

I want change value in "MyProject.Properties.Settings.Default.Property" but give to me error and this error;

Severity Code Description Project File Line Suppression State Error CS0200 Property or indexer 'Settings.Version' cannot be assigned to -- it is read only

How I can solve this problem? Or Which I can try different method?

21.03.2017 EDIT - I SOLVED PROBLEM WITH THIS METHOD

Properties.Settings.Default["Version"] = File.GetLastWriteTime(mainDllPath).ToString("ddMMyyyyhhmmss");
Properties.Settings.Default.Save();
Rusch answered 20/3, 2017 at 14:4 Comment(2)
Why do you want to re-assign a value to the applications configuration-settings? Usually those settings are set from the user of your app, not the program itself.Everyway
If my answer solved your issue I'd appreciate you marking it as accepted, if it hasn't please let me know and I'll try and help furtherGaskell
S
7

From Microsoft:

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.

How To: Write and Persist User Settings at Run Time with C#:

Access the setting and assign it a new value as shown in this example:

Properties.Settings.Default.myColor = Color.AliceBlue;

If you want to persist the changes to the settings between application sessions, call the Save method as shown in this example:

Properties.Settings.Default.Save();

User settings are saved in a file within a subfolder of the user’s local hidden application data folder.

You can find more about using settings in c# here.

Spalding answered 20/3, 2017 at 14:38 Comment(2)
I don't change with your method because I tried before microsoft codes but didn't allow for change in runtime.Rusch
The settings have two different scopes: User and Application. Only your settings with User scope are editable at runtime. To check your settings: Go to Solution Explorer > expand the Properties node of your project > double-click the .settings file. There you can create your own settings with User scope and edit it at runtime, if that is your intention.Spalding
G
3

It sounds like you're trying to edit this read-only property at runtime from code. You simply cannot do this.

To change your version you need to access the project properties through the solution explorer and change it in there.

In this particular case, the version number is added to the metadata of your compiled executeable and needs to be accessible to a host OS without the code actually running. So changing this OTF really would be counterproductive.

Gaskell answered 20/3, 2017 at 14:5 Comment(0)
P
0

scop user:modifiable scop application:read only

Photocopy answered 12/8, 2022 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.