问题
I have a program which I want to store information in a string. I have been using the Properties.Setting.Default.STRINGname
to store the information, which works fine on my PC, and I can see the saved strings (when I go to the settings in the application). But when I take the application to a new PC it losses the strings. Is there a way to be able to edit this information and save it in the app? So basically I need to be able to convert the user setting to application setting, but after the runtime.
var settings = Properties.Settings.Default;
settings.H1 = textbox1.text;
settings.H2 = textbox2.text;
settings.Save();
回答1:
MSDN explicit says something about this:
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.
For this, Application setting will never work for you. However, if you are using a User scoped settings it does work, but soon you change the application from one computer to another (as you say you want to) you will loose the settings as that's another machine (another user-scope)...
There are way to continue to have the same settings, you can do, at least 2 things:
- use a
.config
file to save the settings (it's an XML file) - use a service to host the settings and you can read if user has internet access
What you can't do is
- using only one executable file, save the settings from computer to computer.
回答2:
User settings are compiled differently than Application settings, and thus cannot be converted after compilation. Why not compile with Application Settings?
回答3:
The code you are using should save the user settings. Rememeber that user settings will be saved in the user's data folder. Not in the configuration file where the app was installed (say program files). This is the usual path:
<Profile Directory>\<Company Name>\<App Name>_<Evidence Type>_<Evidence Hash>\<Version>\user.config
See this links form more information
来源:https://stackoverflow.com/questions/10304665/saving-setting-to-a-exe