Best way to persist settings when application exists - c#

I have a Metro app where I need to save some data about the current "session" so that the next time the user launches my application, this session data may be restored. Some of the data is not meant for the user, but to aid in which data should be displayed right away, and which should be displayed at a later time if requested by the user.
I have been using LocalSettings for other things but have just noticed that these settings only appear to last for the lifetime of the application.
How should we be storing settings that need to be saved to the App's data folder?

You can achieve it by binding the data in to certain format , saving it in a file , retrieve it when the app launches.
Create a ApplicationDataContainer.
Initialize a StorageFile with the name you wish.
Serialize your "theme/settings" object using
DataContractSerializer.
Write the content to the StorageFile instance created.
On Application launch:
Deserialize the data.
Populate in the way you want.
You can rather use XML as mentioned by #Lütfullah Kus

you can store log to xml file like "lastform:frmSomething;lastwindow:somewindow..." and you can reload it when app start again.

Related

Perstisting local data after update from app store

I'm developing an app for android and ios with Xamarin.Forms where I have to store data, which the user enters.
The data may never be deleted, except the user deletes them within the app or by deinstalling the app. But the data must be persisted when the user installs an update via the app store.
Also the data (which is a json string) contains no token or other temporary data. Every day the data could be increase by 200-300 string characters.
At the moment I use Application.Current.Properties, but I'm not sure if it fits both of my requirements...
Which framework/approach is the best for it?
Just write your json string into a file. You'll get it back after an update.

How to store information of logged in User in desktop application using c#

We want to keep track of which User is logged in at the moment. Instead uf using for example: Environment.Username we want to know the Username from our database.
We are able to get the Username from the database but we want to store it somewhere. Any solutions?
An example from the question linked below:
Settings.Default["SomeProperty"] = "Some Value";
Settings.Default.Save(); // Saves settings in application configuration file
I recommend to have a glance at this question.
Best practice to save application settings. There are other solutions as well.
They are several solutions:
Using MVVM, create shared user manager service(recommended).
Singleton service.
Application settings.
Static variable.
If your application used by more than one user than you can store data in traditional file like, CSV, XML etc. But using such technique you may not get data security, so for that you could use Binary serialization.

wp8 c# ApplicationSettings

If I do this:
IsolatedStorageSettings.ApplicationSettings["mytype"] = "x-type";
I can later find mytype-setting even if I didn't use Save-method.
Should I use Save for some reason ?
Data written to the IsolatedStorageSettings object is saved when the
application that uses the class is closed. This can occur when the
user closes the Web browser, refreshes a page, or browses away from
the page. If you want your application to write to isolated storage
immediately, you can call the Save method in application code.
From MSDN
Meaning that as long as you don't close your app you'll be fine, but if something happens (imagine you receive a phone call), or if you really close your app the variable will be lost. You should use the Save() method.

app.config not loading when App is on StartUp

I have a problem. If I run my application by clicking on it, it loads the saved app.config settings.
However, I need to have the Application run at Startup. Got this working too, but when it loads it does not load the saved settings - just the default ones.
The first time my Application loads with the default settings I require the user to login to there account through a REST API, grab some data and store it. I then set
Properties.Settings.Default.is_installed = true
but when the app runs in Startup it loads the default (false).
Has anyone experienced this? Any help would be appreciated!
If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.is_installed = true;
Properties.Settings.Default.Save();
Here is MSDN Reference 1.
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.
Here is MSDN Reference 2.
Saving User Settings at Run Time:
Application-scope settings are read only, and can only be changed at
design time or by altering the .exe.config file in
between application sessions. User-scope settings, 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 changes to user settings between application sessions by
calling the Settings.Save method. These settings are saved in the
User.config file.

how many times a file has been downloaded

I am creating a web browser using C#, and I need to get specific data from the web pages that are loaded in my browser.
The pages I am loading is a download scripts. The data I want to get is: the number of times the file has been downloaded.
I want to save this value in text.
What code can I use for this, or where can I start? Any help is appreciated.
Most web browsers have their own storage. Mozilla uses SQLite for some things.
Whenever your app/browser needs to retrieve a remote resource (URL of any kind), simply log it to a database table.
Perhaps use SQLite yourself for this. A decent start would be to create a history table like this:
URL --varchar(max)
LastAccessed --datetime
TotalRequests --int
If it is a file that the users will be downloading, you could add a global static int and increment it every time the file is downloaded.

Categories

Resources