Windows 10 Universal App - Best way to store static application settings - c#

I would like to store some configurable data (hosts, keys etc.) in my universal app. The data would be defined before running the app and never changed at runtime. What is the best way to achieve this? I've come across a similar question, but there must be some built-in mechanism.

Here you will find some info: Store and retrieve settings and other app data
If you want to have a file with settings that are known at compile time you can add a json or xml file in your solution and use it as a config file in code.

Related

What is the easiest way to save data of application even after closing it [duplicate]

This question already has answers here:
Preserve data between application executions
(4 answers)
Closed 4 years ago.
I want to use a data saving feature in several of my simple winform apps. Is there a way to simply save a value of variables somewhere (Database, Textfile) and load it after each start of the app?
And what is the simplest way to do so?
There are too many ways to save your data after closing, some of them :
Settings
to add it programmatically, you can do :
System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("Sample1");
Properties.Settings.Default["Sample1"] = SomeStringValue;
Properties.Settings.Default.Save();
Database
You can save your data in database tables if they are large
XML File
if you have a medium embedded data the xml file is the way to go.
Finally, its all depends on what data you want to store.
You can use one of the options below
Use the settings that come with visual studio to save your variable and load them back when you start the app the next time.Like below
Go to your project properties and go to settings then add a new settings . You can add as many settings as you want using the settings designer. They can be of different variable types (string, color,into,etc)
To change a setting from code, use
//we created a setting named 'mysetting' of type to string
Properties.Settings.Default.mysetting = "test string";
//Save the setting
Properties.Settings.Default.Save();
//To get the value of a setting, use
string mystr = Properties.Settings. Default.mysetting;
You can simply write the value of the variable to a text file and read it back on startup.
One of the easiest solution for your would be to use the Application Settings which inside Visual Studio, you can easily do much of the work in the design time as well.
Starting with the .NET Framework 2.0, you can create and access values
that are persisted between application execution sessions. These
values are called settings. Settings can represent user preferences,
or valuable information the application needs to use. For example, you
might create a series of settings that store user preferences for the
color scheme of an application. Or you might store the connection
string that specifies a database that your application uses. Settings
allow you to both persist information that is critical to the
application outside the code, and to create profiles that store the
preferences of individual users.
Read the documentation for examples.
You can use System.Configuration.Settings which will save your configs into YourProgram.exe.config in xml format, but YOU WILL NEED TO CALL Save() MANUALLY TO SAVE THE MODIFIED CONFIG. In .Net programming, I consider it very convenient.
You can also define your own config file, a simple approach can be using json.
Another approach is to use a local database like sqlite(recommended) or access(less recommended) an define a table called 'config' to save all your config fields.
The least recommended approach is to save your config into windows registery.WHICH WILL MAKE YOUR USER VERY ANNOYED.

How to get an Application Settings shared to all users that could be changed at run time

I need some setting of an application that will be shared among all users of the computer, but could also be changed at at run time. That seam simple, but according to the Application Settings MSDN article, it's either one or the other.
There are two types of application settings, based on scope:
Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application. Therefore, users cannot change them at run time.
User-scoped settings can be used for information such as persisting the last position of a form or a font preference. Users can change these values at run time.
I could write code to edit the app.config XML file, but since it's located in the program directory, it's protected under windows 7. So this is not possible without elevating the program or playing with NTFS rights.
So I need the configuration file to be written in a common folder like System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).
But this is a fairly common requirement!
So, I'm wondering if there a simple way of achieving this without reinventing the wheel, or if I have to write my own Setting Manager.
After reading the answers here and playing with the ConfigurationManager.Open methods I ended up with the following:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApp", "MyApp.config");
Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None);
The nice part is the file doesn't have to exist, and if you call Save() on it, it will create the folder and the file for you. AppSettings["key"] didn't work though so I had to use
MyAppConfig.AppSettings.Settings["key"].Value
to read and write an existing value and
MyAppConfig.AppSettings.Settings.Add("key", value)
to add a new value.
I have had a similar problem and ended up writing my own settings class. It was very basic. I created a Settings class with the properties I needed, and a SettingsManager with Save() and Load() methods that simply serialized/deserialized the object via XmlSerializer into/from a file.
Yes, it is your own code, but it is very simple code, takes less time than trying to figure out whether there is a component providing what you need and how to customize it.
The Application Settings infrastructure does not support this - only non-editable application data and user-scoped data are supported. You can easily read and write your own XML into the CommonApplicationData folders, however, instead of using the application data.

Programatically updating resource files in windows phone 7

I am using Resource file(.Resx format) to store configuration settings for a WP7 application. I want to edit(and optionally create) resource files programatically in code. I googled around, and found that some of the classes are not available in WP7 SDK, which are present as a part of usual .Net framework.
So, my question is that, is there a way to edit the resx files in code?
I am only storing strings (key, value pair) as settings in resource files. So, is there something else I can do to store these values.
Reference:: System.Resources namespace in:
1) .Net framework 4 - http://msdn.microsoft.com/en-us/library/system.resources.aspx
2) WP7 SDK - http://msdn.microsoft.com/en-us/library/system.resources(v=VS.95).aspx
WP7 Newbie Here!!, Help is much appreciated.
Update:
My Use case for the above scenario.
1) It's a read frequently, update once in a while configuration settings.
2) I need to access about 300+ such settings, and want to do as much minimum (directly invoking I/O functions) as possible.
You can store them in IsolatedStorageSettings which is very well managed in WP7 applications.
IsolatedStorageSettings.ApplicationSettings["Key"] = object;
Just one point, beware that IsolatedStorageSettings is persisted to phone when your application is tombestoned or exitted. In case of unexpected exit it will not persist on the phone. Although you can always enforce persisting it using the following code:
IsolatedStorageSettings.ApplicationSettings.Save();

Where to store user preferences for a C# Windows app

Can anyone provide a best-practices example for where to store user preferences for a C# Windows app?
So far I've heard a number of options:
Some people are saying to store it in
SQLite. Is SQLite bundled with .NET
2.0 and immediately available for use to me?
Others have said to use the built-in
Application Settings... but I've
heard that the per-user settings here
disappear if you upgrade the app (an
obvious problem).
I've also considered just storing it
in a .xml file somewhere on disk...
but where it the "correct" place to
store that .xml file for the user?
SQLite is not included with .NET2, but you could ship it with your application
The built-in settings system works fine for simple apps - you do need to add a couple of lines of boilerplate to deal with version changes but it's not complicated.
You could put your xml file here:
Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData)
There is a lot of 'it depends' about your question, as you don't say how much of what types of data you need to store, nor if you have any other reasons to care where it goes.
The per-user settings "disappearing" can be solved thus: .NET ApplicationSettingsBase Should I call Upgrade() every time I load?
Storing user application settings in isolated storage seems to be a best practice.
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage(v=vs.80).aspx
Storing settings as an xml file is perfectly fine.

C# Application Configuration Data

Hey, I want to store application configuration data. Currently, I'm using INIs but they are too old and now I want to try something new. I'm on .NET platform using C#.
What solutions do I have? app.configs?
The scenario here is like that, I want to let other programs read the configuration data.
Yes, the various .config files (app.config, web.config, etc..) have replaced INI and, to a certain extent, the Registry as the preferred means of storing configuration data for your application.
Any program, .Net or otherwise, can read those files. .Net has the ConfigurationManager class and other languages can simply do XML parsing to get the values they need.
The standard approach with .NET is to use app.config files for application settings and user.config files for user specific settings (the location varies between XP and Vista though).
There's nothing to stop other programs reading the configuration data, you just need to have a configuration setting in the second (or third) application that tells it where to look.
The app.config would be the preferred way of doing things in .NET. Should work fine to read from other applications as well, as long as you give them the right path to the file. And it's just an XML file, so you can read it from non .NET apps as well.
If you're using Visual Studio, you can use Application Settings for this purpose. Just open the automatically added Settings.settings or create another one. They will be automatically available in Properties.Settings.Default and are stored as XML. You can also have multiple settings files for different purposes.
This is a Visual Studio concept rather than a .NET concept.
I often use an SQL table to hold my application settings. I create a singleton class, ususally called AppSettings, that I load with data from the table. The AppSettings class is then used to get the config values instead of directly accessing the config files. For ASP.Net apps, I instantiate the AppSettings class in the Application_Start event in Global.asax.cs.
Doing this gives me a way to allow the user to control some of the app settings, e.g. an email address for notifications. It also can simplify things when maintaining prod, QA, and dev versions of the app (assuming you have separate database instances for each)

Categories

Resources