#

Speichern und Lesen von Settings in Windows Application

 

WinApp, UWP,

 

 

 

Beim Speichern von Settings

Ein Bild, das Text enthält.
Automatisch generierte Beschreibung

 

//create data based on model class

            UserData_Model userdata_01 = new UserData_Model()

            {

                ip = "127.0.0.1",

                user = "raimund",

                path = "D:"

            };

 

            //convert to named settings

            ApplicationDataCompositeValue userdata_setting =new ApplicationDataCompositeValue();

            userdata_setting["ip"] = userdata_01.ip;

            userdata_setting["user"] = userdata_01.user;

            userdata_setting["path"] = userdata_01.path;

 

            //save setting

            _users_Settings_container.Values["userdata_" + 1] = userdata_setting;

 

            ctlLog.Text = DateTime.Now + " saved "  ;

 

 

Beim Laden von Settings

Ein Bild, das Text enthält.
Automatisch generierte Beschreibung

 

Beim lesen von Settings

//ApplicationDataCompositeValue userdata_setting = new ApplicationDataCompositeValue();

            //save setting

            ApplicationDataCompositeValue userdata_setting = (ApplicationDataCompositeValue) _users_Settings_container.Values["userdata_" + 1] ;

           

            //create data based on model class

            UserData_Model userdata_01 = new UserData_Model();

            userdata_01.ip = userdata_setting["ip"].ToString();

            userdata_01.user = userdata_setting["user"].ToString();

            userdata_01.path = userdata_setting["path"].ToString();

 

 

            //output to log

            ctlLog.Text  = DateAndTime.Now + " " + "ip=" + userdata_01.ip + " user=" + userdata_01.user + " path= " + userdata_01.path + Environment.NewLine + ctlLog.Text  ;

        }

 

 

MainWindow.cs

// Copyright (c) Microsoft Corporation and Contributors.

// Licensed under the MIT License.

 

using Microsoft.UI.Xaml;

using Microsoft.VisualBasic;

using System;

using Windows.Storage;

 

namespace winapp_settings_model

{

    /// <summary>

    /// demo to show settings

    /// </summary>

    public sealed partial class MainWindow : Window

    {

 

        private const string UsersKey = "users";

        private ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

        ApplicationDataContainer _users_Settings_container = null;

       

        public MainWindow()

        {

            this.InitializeComponent();

            _users_Settings_container = localSettings.CreateContainer(UsersKey, ApplicationDataCreateDisposition.Always);

        }

 

        private void btnSaveSetting_Click(object sender, RoutedEventArgs e)

        {

            //create data based on model class

            UserData_Model userdata_01 = new UserData_Model()

            {

                ip = "127.0.0.1",

                user = "raimund",

                path = "D:"

            };

 

            //convert to named settings

            ApplicationDataCompositeValue userdata_setting =new ApplicationDataCompositeValue();

            userdata_setting["ip"] = userdata_01.ip;

            userdata_setting["user"] = userdata_01.user;

            userdata_setting["path"] = userdata_01.path;

 

            //save setting

            _users_Settings_container.Values["userdata_" + 1] = userdata_setting;

 

            ctlLog.Text = DateTime.Now + " saved "  ;

        }

 

        private void btnLoadSetting_Click(object sender, RoutedEventArgs e)

        {

            //ApplicationDataCompositeValue userdata_setting = new ApplicationDataCompositeValue();

            //save setting

            ApplicationDataCompositeValue userdata_setting = (ApplicationDataCompositeValue) _users_Settings_container.Values["userdata_" + 1] ;

           

            //create data based on model class

            UserData_Model userdata_01 = new UserData_Model();

            userdata_01.ip = userdata_setting["ip"].ToString();

            userdata_01.user = userdata_setting["user"].ToString();

            userdata_01.path = userdata_setting["path"].ToString();

 

 

            //output to log

            ctlLog.Text  = DateAndTime.Now + " " + "ip=" + userdata_01.ip + " user=" + userdata_01.user + " path= " + userdata_01.path + Environment.NewLine + ctlLog.Text  ;

        }

 

 

 

 

 

 

 

 

        public class UserData_Model

        {

            public string ip;

            public string user;

            public string path;

        }

    }

}

 

 

 

MainWindow.xaml

<!-- Copyright (c) Microsoft Corporation and Contributors. -->

<!-- Licensed under the MIT License. -->

 

<Window

    x:Class="winapp_settings_model.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:winapp_settings_model"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <StackPanel Orientation="Vertical" HorizontalAlignment="Left"  VerticalAlignment="Top">

        <StackPanel Orientation="Horizontal">

            <Button x:Name="btnSaveSetting" Click="btnSaveSetting_Click">Save Setting</Button>

            <Button x:Name="btnLoadSetting" Click="btnLoadSetting_Click">Load Setting</Button>

        </StackPanel>

        <TextBlock x:Name="ctlLog"></TextBlock>

       

    </StackPanel>

</Window>

 

 

Mobile

.

soap2day