C# Properties.Settings 属性 没有 setter 任何值
C# Properties.Settings Property doesn't setter any value
我想更改 "MyProject.Properties.Settings.Default.Property" 中的值,但给我错误和这个错误;
Severity Code Description Project File Line Suppression State
Error CS0200 Property or indexer 'Settings.Version' cannot be assigned
to -- it is read only
我该如何解决这个问题?或者我可以尝试哪些不同的方法?
21.03.2017 编辑 - 我用这种方法解决了问题
Properties.Settings.Default["Version"] = File.GetLastWriteTime(mainDllPath).ToString("ddMMyyyyhhmmss");
Properties.Settings.Default.Save();
听起来您正试图在运行时通过代码编辑此只读 属性。你根本无法做到这一点。
要更改您的版本,您需要通过解决方案资源管理器访问项目属性并在其中进行更改。
在这种特殊情况下,版本号被添加到编译后的可执行文件的元数据中,并且需要主机 OS 可以访问而实际上没有代码 运行。所以改变这个OTF真的会适得其反。
来自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.
我想更改 "MyProject.Properties.Settings.Default.Property" 中的值,但给我错误和这个错误;
Severity Code Description Project File Line Suppression State Error CS0200 Property or indexer 'Settings.Version' cannot be assigned to -- it is read only
我该如何解决这个问题?或者我可以尝试哪些不同的方法?
21.03.2017 编辑 - 我用这种方法解决了问题
Properties.Settings.Default["Version"] = File.GetLastWriteTime(mainDllPath).ToString("ddMMyyyyhhmmss");
Properties.Settings.Default.Save();
听起来您正试图在运行时通过代码编辑此只读 属性。你根本无法做到这一点。
要更改您的版本,您需要通过解决方案资源管理器访问项目属性并在其中进行更改。
在这种特殊情况下,版本号被添加到编译后的可执行文件的元数据中,并且需要主机 OS 可以访问而实际上没有代码 运行。所以改变这个OTF真的会适得其反。
来自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.