Partager via


모다의 윈도우폰7 뚝딱 팩토리(6)-윈도우폰의 저장소

 

 

한국마이크로소프트에서 초급 스마트폰 개발자 분들을 위해 공개하는 모다의 윈도우폰7 뚝딱 팩토리 여섯번째 영상!

 

윈도우폰의 저장소는 일반적인 PC 환경의 저장소와 조금 다른 형태를 띄고 있습니다. “격리된 저장소”라고 불리는 이 곳은 어플리케이션 상호간 데이터 접근이 엄격하게 제한되어있습니다. 자신이 가진 저장소만 접근할 수 있고, 남의 저장소에 가서 파일을 열람하거나 변경하는 것이 불가능한 구조입니다.

 

AP_Con_IStorage2

(From MSDN Library)

 

기존의 윈도우 어플리케이션에서 파일 핸들링 하는 것과 유사한 부분이 많아서 기존에 파일시스템 관련 프로그래밍을 해 보신 분들은 쉽게 따라하실 수 있는 구조이며, 처음에 공간할당을 받는 부분만 주의 해 주시면 됩니다.

 

격리된 저장소에 대해 조금 더 자세하게 알고 싶으신 분들은 MSDN라이브러리의 IsolatedStorage Overview를 참고해 주시기 바랍니다!

 

격리된 저장소에 파일 저장하고 읽기

    1:  using System;
    2:  using System.Collections.Generic;
    3:  using System.Linq;
    4:  using System.Net;
    5:  using System.Windows;
    6:  using System.Windows.Controls;
    7:  using System.Windows.Documents;
    8:  using System.Windows.Input;
    9:  using System.Windows.Media;
   10:  using System.Windows.Media.Animation;
   11:  using System.Windows.Shapes;
   12:  using Microsoft.Phone.Controls;
   13:   
   14:  using System.IO;
   15:  using System.IO.IsolatedStorage;
   16:   
   17:  namespace isoStorage1
   18:  {
   19:      public partial class MainPage : PhoneApplicationPage
   20:      {
   21:          // Constructor
   22:          public MainPage()
   23:          {
   24:              InitializeComponent();
   25:          }
   26:   
   27:          private void button1_Click(object sender, RoutedEventArgs e)
   28:          {
   29:              using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
   30:              {
   31:                  isf.CreateDirectory("Moda");
   32:                  IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream("Moda\\comment.txt", FileMode.Create, isf);
   33:                  StreamWriter writer = new StreamWriter(isfStream);
   34:   
   35:                  writer.WriteLine(textBoxSave.Text);
   36:                  writer.Close();
   37:                  isfStream.Close();
   38:              }
   39:   
   40:          }
   41:   
   42:          private void button2_Click(object sender, RoutedEventArgs e)
   43:          {
   44:              using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
   45:              {
   46:                  IsolatedStorageFileStream isfStream = 
   47:                      new IsolatedStorageFileStream("Moda\\comment.txt", FileMode.Open, isf);
   48:                  try
   49:                  {
   50:                      StreamReader reader = new StreamReader(isfStream);
   51:                      textBlockLoad.Text = reader.ReadToEnd();
   52:                      reader.Close();
   53:                      isfStream.Close();
   54:                  }
   55:                  catch
   56:                  {
   57:                      MessageBox.Show("error");
   58:                  }
   59:              }
   60:   
   61:          }
   62:      }
   63:  }

 

격리된 저장소에 설정값 저장하고 읽기

    1:  using System;
    2:  using System.Collections.Generic;
    3:  using System.Linq;
    4:  using System.Net;
    5:  using System.Windows;
    6:  using System.Windows.Controls;
    7:  using System.Windows.Documents;
    8:  using System.Windows.Input;
    9:  using System.Windows.Media;
   10:  using System.Windows.Media.Animation;
   11:  using System.Windows.Shapes;
   12:  using Microsoft.Phone.Controls;
   13:   
   14:  using System.IO;
   15:  using System.IO.IsolatedStorage;
   16:   
   17:  namespace isoStorage2
   18:  {
   19:      public partial class MainPage : PhoneApplicationPage
   20:      {
   21:          // Constructor
   22:          public MainPage()
   23:          {
   24:              InitializeComponent();
   25:          }
   26:   
   27:          private void button1_Click(object sender, RoutedEventArgs e)
   28:          {
   29:              IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
   30:              iss.Add("WP7", "COOL");
   31:              iss.Save();
   32:              MessageBox.Show("Saved");
   33:          }
   34:   
   35:          private void button2_Click(object sender, RoutedEventArgs e)
   36:          {
   37:              IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
   38:              string settingsText;
   39:   
   40:              try
   41:              {
   42:                  iss.TryGetValue("WP7", out settingsText);
   43:                  textBlock1.Text = settingsText;
   44:              }
   45:              catch
   46:              {
   47:                  MessageBox.Show("error");
   48:              }
   49:          }
   50:      }
   51:  }