참조 : https://youtu.be/MwftpaA4dNM?list=PL_fV1knZRgi7Uu6GDZi5SzNvjRiXT4Ivd

 

* 프로그램 종료시 입력/수정된 데이터를 격리된저장소(Isolated Storage)에 저장

* 프로그램 재시작시 종료전 데이터를 다시 로드함

 

<Window x:Class="AboutIsolatedStorage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AboutIsolatedStorage"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="400"
        Closed="Window_Closed"
        Initialized="Window_Initialized">
    <StackPanel>
        <TextBlock>추가할 친구</TextBlock>
        <TextBox x:Name="txtNewFrd"></TextBox>
        <Button Click="Button_Click">추가</Button>
        <ListBox x:Name="listFrd"></ListBox>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AboutIsolatedStorage
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(txtNewFrd.Text != String.Empty)
            {
                AddFriend(txtNewFrd.Text);
                txtNewFrd.Text = string.Empty;
            }
        }

        private void AddFriend(string text)
        {
            ListViewItem lvl = new ListViewItem();
            lvl.Content = text;
            listFrd.Items.Add(lvl);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            IsolatedStorageFile isofile = IsolatedStorageFile.GetUserStoreForAssembly();

            using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream("listfrd", System.IO.FileMode.Create, isofile))
            {
                using(StreamWriter sw = new StreamWriter(stream))
                {
                    foreach(ListViewItem lvl in listFrd.Items)
                    {
                        sw.WriteLine(lvl.Content);
                    }
                }

            }
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            IsolatedStorageFile isofile = IsolatedStorageFile.GetUserStoreForAssembly();

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("listfrd", System.IO.FileMode.OpenOrCreate, isofile))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    while (!sr.EndOfStream)
                    {
                        AddFriend(sr.ReadLine());
                    }
                }

            }
        }
    }
}

 

 

 

 

 

 

 

 

 

+ Recent posts