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();
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.
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.
scop user:modifiable
scop application:read only
Related
I know there are several other similar questions and answers but they don't work in my scenario.
My application gets the path to a log4net.config file inside a folder which I provide as a parameter. Once the exact path is acquired. I initialize my XmlConfigurator as such:
XmlConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
Then, I receive the current logging level using the following code
string currentLevel = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level.ToString();
which retrieves the correct current log level for me from that file (I do this to check whether the Configurator is linking to the file and working properly).
The value I am retrieving from the previous line of code is the very level I need to change. For this, I use the following code, proposed as solutions for other similar questions:
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level = newLevel;
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);
where newLevel is the variable storing the level I want to change it to, e.g.
newLevel = Level.Fatal;
After a lot of debugging, I realized that if I receive the current level again after the previous code using the same
string currentLevel = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level.ToString();
statement again, it does in fact give me the newly set value of the logging level.
However, some why, there are no changes made to the actual config file when I open and view it in a text editor. Also, once I restart my application, or if I write another XmlConfigurator.ConfigureAndWatch (exactly the same as before) statement before checking the newly set log level, the log level shows as if it is unchanged.
Please help me sort out my problem. Thank you so much.
If you change configuration in your program, it is not reflected in the configuration file. If your application is monitoring your configuration, you can change the log level by editing the configuration file directly. It will be picked up by log4net.
I have a C# program with various setting stored in the program project settings. As it is a command line program, these settings will be changed by the user in the user.config file. I can't work out how to force it to save all the default values.
Calling Properties.Settings.Default.Save(); only seems to work when I update a property in the program, which defeats the point of a persistent settings file.
If I run
Properties.Settings.Default.SomeSetting = "Help";
Properties.Settings.Default.Save();
In the config file I will see something like
< setting name="SomeSetting" serializeAs="String">
<value>Help</value>
< /setting>
What I want to happens is if the config file doesn't exist, create it and store all the settings. Then the user can change them if needed.
The way I managed to solve it was by modifying some existing code I had in a different project to handle version upgrades.
When the program runs:
if (PropertiesSettings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
reloadSettings();
}
// Assign settings to themselves and save
// This keeps settings the same as the defaults even when upgrading but ensures all settings appear in the config file
private void reloadSettings()
{
Properties.Settings.Default.Setting1 = Properties.Settings.Default.Setting1;
Properties.Settings.Default.Setting2 = Properties.Settings.Default.Setting2;
//etc
Properties.Settings.Default.Save();
}
The UpgradeRequired setting should be set to True by default, so a new version with no config will be forced to run the first if statement. The .NET Properties.Settings.Default.Upgrade(); should then take care of finding old config files and loading the old settings.
Settings are saved somewhere in user AppData folder specifically for a given process. For example, when the application is run under the debugger, you get different settings than when running stand-alone.
I would guess that there is no easy way to share settings across application using those properties settings. That code is quite old (.NET 2.0 or even before) so I think it is not designed to be easily customizable as it is often the case with more recent code.
I think that settings are good for things that are specific to a given application and if data need to be shared across application, then you should manage your own files.
Usually, one would use either XML or JSON based file and the data would be stored (by default) either under a subdirectory of My Documents if the data is intended to be visible to users or under a subdirectory of user application data directory if a regular user should not see those files.
In my C# WPF application, I want to incorporate something like 'last used file remember feature'
So, if previously the user has opened some file (and remembered it), the next time on opening the application, the file should open automatically.
Other than writing the path of the file, is there any other way to this?
If I have to write the path to the file, what's the location mostly used to store these temp remembered file paths and keep away from user? (some environment folder?)
You can specify User Settings to store some information. In the Solution Explorer go to your project and in the folder Properties, double-click the settings-file. In here you can set the Name, Type, Scope and Value. Just set the Name to "FileName", Type to "string", scope to "User" and leave the Value empty or set a default value if you want. Save the settings-file. In code you can set and get the value like this:
//get
var name = Properties.Settings.Default.FileName;
//set
Properties.Settings.Default.FileName = "....";
Properties.Settings.Default.Save();
Hope this helps!
I think you can use ont of this .NET features:
User Settings
Application Settings
They save settings with different scope. I guess you can use application settings.
Usually there are three location available for this kind of configuration.
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
The first is to be used if the configuration applies to the whole application
The second is to be used if the configuration applies only to the current user (roaming)
The third is to be used if the configuration applies only to the current user (local)
In each case you should create a subfolder in these paths with the name of your company and then the name of your product. Never save directly there.
I prefer to stay away from changing config files from code although I must admit it is very convenient
I have a windows app that takes advantage of the Settings. I have a few user settings that the user can set manually, they are set as User scope. The save method works fine, however if the application is closed and then reopened, the values are set back to the defaults.
here is one example of my code to save:
Properties.Settings.Default["LocalDefaultPrinter"] = Default_Printer;
Properties.Settings.Default.Save();
what could be the problem?
I'm not sure if this is the problem, but I'm used to doing it using the designer generated properties:
Properties.Settings.Default.LocalDefaultPrinter = Default_Printer;
Properties.Settings.Default.Save();
EDIT: Well, that's not the problem - I was able to save fine using your approach provided the setting was "User" scoped. However, if you scope this as 'Application', you won't be able to save the value because that gets stored in your application.config file (application properties don't get saved back to that file on .Save).
When you alter user settings programmatically, you aren't altering the application config file. You are altering a copy of those settings in the user's profile that get merged with the application config at runtime.
Have you checked the user's profile folder for your altered settings? Are they there? If so, your code is functioning properly.
Thanks.
-Jason
In addition to these possibilities, understand that user settings are scoped to the version number of the executable. So, if you automatically increment your build or change your build number during debugging, you will lose any settings already updated by a prior version of the application.
For anyone having problems in WPF.
Call the Settings.Default.Upgrade() to retrieve the non-default values when you want to use the settings you saved.
Nevermind everyone. I forgot I had a method in the code that checked if the user set printer was the same as the system set printer and if not then change it to the system printer. Took that method out and all is well.
Application scoped settings are read only whereas user scoped settings are read/write. Calling Save() will persist the user scoped settings between application sessions. If LocalDefaultPrinter is application scoped this will not persist between sessions.
This post provides information on how to make use of the settings in its entirety.
I just experienced a similar problem with one specific key in user settings.
To solve it, I tried deleting the key and adding it back. Final solution, I added a new key with a new name it worked. Dumb solution but it worked.
Im building a small project that uses some settings features. I could change the DB Path and some other textfields.
Whats the best way in c# to save these settings? Is there some built in config or ini file that can be changed under runtime? Because if I change the DB Path i need it to do this without restarting the program. I know i ofc can build a simple textdoc and read from but I whant to use some standard c# way.
Thx for anyfeedback.
/Marthin
If you have a look in the project property pages you can add a settings file.
To use the settings in code you would do something like:
Properties.Settings.Default.SettingName
Do bare in mind though that these settings are local and would need to be specified on each machine
Here is a link to the settings class on MSDN
http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
if you create a new c# windows form application you have by default the properties, try to expand the node in solution explorer, you will find the settings file where you could add your values, at runtime you could retrieve them and change them, after changing them you save them in this way:
Properties.Settings.Default.Save();