Small, simple local data storage for storing user settings

I have this tiny C # winforms application that will NOT grow anymore. These are just two input fields and a button.

I want to know if you know that you know a way to store valuesthat the user enters in the local data warehouse . You can read 10 records as more data for this scenario.

Requirements

  • It should not require any configuration from the database. (creating a table, etc.)

  • I should just give it an object, and it should store it, I don’t want to waste time on it.

  • Data should be easily accessible for recovery.

  • I want to be able to reuse this thing for every small application that I create, like this.

My ideas

  • The POCO object that will be XML-serialized and saved in the Local Settings folder. After loading the application, this file is deserialized back to the POCO object.

  • OODBMS: I have no experience with them, but I always thought that they consist of one DLL, so it would be easy to package them using a program.

  • I once, a long time ago, created an application that stores user settings in the registry. I don’t know, although it is still appreciated.

What do you think is the best approach?

Code examples are much appreciated!

+3
source share
3 answers

I took into account both answers and built the following:

public static class IsolatedStorageExtensions
{
    public static void SaveObject(this IsolatedStorage isoStorage, object obj, string fileName)
    {
        IsolatedStorageFileStream writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(writeStream, obj);
        writeStream.Flush();
        writeStream.Close();
    }

    public static T LoadObject<T>(this IsolatedStorage isoStorage, string fileName)
    {
        IsolatedStorageFileStream readStream = new IsolatedStorageFileStream(fileName, FileMode.Open);
        BinaryFormatter formatter = new BinaryFormatter();
        T readData = (T)formatter.Deserialize(readStream);
        readStream.Flush();
        readStream.Close();

        return readData;
    }
}

POCO-, :

[Serializable]
internal class DataStoreContainer
{
    public DataStoreContainer()
    {
        UserIDs = new List<int>();
    }

    public List<int> UserIDs { get; set; }
}

:

private IsolatedStorageFile _isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
private DataStoreContainer _data = new DataStoreContainer();
private const string FILENAME = "MyAppName.dat";

, :

_data = _isoStore.LoadObject<DataStoreContainer>(FILENAME);

:

_isoStore.SaveObject(_data, FILENAME);
+3

? , ( , , ). , . .

+2

, 10 , # 1 β„–1, ... , , , , , 10 - , .

, , db4.

+1

Source: https://habr.com/ru/post/1779431/


All Articles