#

Speichern und Lesen in eine Local Datei

 

WinApp 2023

https://youtu.be/hdXJshk-oZc

Speichern

StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", CreationCollisionOption.ReplaceExisting);

await FileIO.WriteTextAsync(sampleFile, sText);

 

 

Lesen

StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");

String sText = await FileIO.ReadTextAsync(sampleFile);

 

Pfad:
C:\Users\Raimund\AppData\Local\Packages\...app-guid…\LocalState\dataFile.txt

 

Notwendig:

Windows.Storage

using Windows.Storage;

..

..

private Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

 

 

 

 

using Microsoft.UI.Xaml;

using System;

using Windows.Storage;

 

namespace winapp_settings_file

{

 

    public sealed partial class MainWindow : Window

    {

        private Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

 

 

        public MainWindow()

        {

            this.InitializeComponent();

        }

 

        private async void btnSave_Click(object sender, RoutedEventArgs e)

        {

            var sText = ctlTextInput.Text;

 

            StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteTextAsync(sampleFile, sText);

        }

 

        private async void btnLoad_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");

                String sText = await FileIO.ReadTextAsync(sampleFile);

                // Data is contained in timestamp

                ctlLog.Text = DateTime.Now + " Read: " + sText + Environment.NewLine + ctlLog.Text;

 

                string sPath= sampleFile.Path;

                ctlLog.Text = DateTime.Now + " Filename: " + sPath + Environment.NewLine + ctlLog.Text;

            }

            catch (Exception)

            {

                // Timestamp not found

            }

        }

    }

}

 

 

Mobile

.

123movies