블로그 이미지
내게 능력 주시는 자 안에서 내가 모든것을 할수 있느니라 - 빌립보서 4 : 13 - happydong

카테고리

Happydong (1363)
프로그래밍 (156)
MUSIC (16)
인물 (3)
Utility (10)
세미나 소식&내용 (22)
IT뉴스 (18)
운동 (830)
CAFE (10)
Life (282)
Total
Today
Yesterday

'IsolagedStorage'에 해당되는 글 1건

  1. 2008.11.03 [Silverlight] IsolatedStorage 클래스



 IsolatedSterage는 무엇??

 Silverlight 애플리케이션을 만들다 보니깐 데이터를 임시적으로 저장할 필요를 느끼게 되었는데요. 알아 보니깐
IsolatedStorage 란것이 있다는 것을 알았어요. 일종의 가상공간(저장공간)이라고 생각하면 될 것 같아요.
그럼 IsolatedStorage를 어떻게 사용해야 하는지 알아 보도록 할게요. 혹시 전에 ASP.NET으로 파일을 생성하고 써보고 그랬더라면 더 쉽지 이해 할수 있지 않을까 생각이 듯네요. 전에는 FileInfo 클래스를 이용해서 파일을 생성했잖아요~!이것도 비슷합니다. 역시 열번의 말보다는 한번의 코드를 보는 것이 이해하는데 빠르겠지요^^
간단하게 Silverlight 예제 코드를 만들어 봤어요.


Page.xaml

<UserControl x:Class="IsolatedStorage_Test.Page"

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

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

Width="400" Height="300">

    <Grid x:Name="LayoutRoot" Background="White">

        <!-- 테스트를 위한 버튼 생성 -->

        <!파일생성 버튼 -->

        <Button Height="21" HorizontalAlignment="Left" Margin="24,23,0,0" VerticalAlignment="Top" Width="200" Content="Isolated Storage 생성" x:Name="btnsetfileinfo"/>
        <!—정보확인 버튼 -->

        <Button Height="21" HorizontalAlignment="Left" Margin="24,48,0,0" VerticalAlignment="Top" Width="200" Content="Isolated Storage 정보확인" x:Name="btngetfileinfo"/>

         <!—메세지 표시 텍스트블럭-->
   
   
<TextBlock Margin="24,0,176,8" Text="TextBlock" TextWrapping="Wrap" x:Name="txbText" VerticalAlignment="Bottom" Height="120"/>

        <!파일삭제 버튼 -->
   
   
<Button Height="21" Margin="24,73,176,0" VerticalAlignment="Top" Content="Isolated Storage 파일삭제" x:Name="btnfiledelete"/>

    </Grid>

</UserControl>


위 코드는 간한하게 버튼 3개와 텍스트블럭 1개로 이루어진 XAML 코드예요. 이 거는 간한하게 블랜드를 이용해서 만들수 있어요. 그리고 이벤트만 정의해 주면되겠지요.그럼  아래 .cs 코드를 보면서 설명하도록 할게요.


Page.xaml.cs

// Linq To XML 사용

using System.Xml.Linq;

// IsolatedStorage 사용

using System.IO.IsolatedStorage;

using System.IO;

namespace IsolatedStorage_Test

{

public partial class Page : UserControl

{

public Page()

{

InitializeComponent();

 

// Button Event

      // 파일쓰기 이벤트

btnsetfileinfo.Click += new RoutedEventHandler(btnsetfileinfo_Click)
// 파일읽기 이벤트

btngetfileinfo.Click += new RoutedEventHandler(btngetfileinfo_Click);
// 파일삭제 이벤트

btnfiledelete.Click += new RoutedEventHandler(btnfiledelete_Click);

}

 

/// <summary>

/// 파일 삭제

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

void btnfiledelete_Click(object sender, RoutedEventArgs e)

{

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

{

// 파일있는지 확인

if (isf.FileExists("Test.xml"))

{

txbText.Text = "파일 있었으나 지금 삭제";

isf.DeleteFile("Test.xml");

}

else

txbText.Text = "파일없음";

}

}

 

    /// <summary>

    /// 파일 읽기

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

void btngetfileinfo_Click(object sender, RoutedEventArgs e)

{

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

{

// 파일 있는지 확인

if (isf.FileExists("Test.xml"))

{

XDocument xdocu = XDocument.Load(isf.OpenFile("MyPolder.xml", FileMode.Open));

var query = from el in xdocu.Descendants("Root")

select new

{

t = el.Element("Test").Value

};

 

string str = string.Empty;

 

foreach (var qy in query)

{

str = qy.t;

}

 

txbText.Text = str;

}

else

{

txbText.Text = "파일없음";

}

}

}

 

/// <summary>

/// 파일 쓰기 (Linq To Xml 쓰기)

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

void btnsetfileinfo_Click(object sender, RoutedEventArgs e)

{

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

{

using (IsolatedStorageFileStream isfstream = new IsolatedStorageFileStream("Test.xml", FileMode.Create, isf))

{

using (StreamWriter sw = new StreamWriter(isfstream))

{

XElement element = new XElement("Root", new XElement("Test", "test..."));

sw.Write(element);

}

}

}

}

}

}


위 3개의 버튼이벤트는 각각 파일 생성, 읽기, 삭제 로 이루어 져있어요. 저는 XML파일을 만들어서 저장해 봤어요. XML파일로 저장해서 많이쓰지 않을까 해서...^^;; 이참에 Linq To XML도 써보려고요. 아무튼 IsolateStorageFile 클래스를 이용해서 파일을 생성합니다. IsolateStorageFile 클래스의 CreateFile 메소드를 이용해서 파일을 생성할수 있어요.FileInfo 클래스를 이용해서 파일을 생성할때와 비슷하다는 생각이 드네요^^. 이렇게 파일을 생성하고 StreamWriter 클래스를 이용해서 파일의 내용을 작성합니다. 그리고 파일을 읽을 때는 StreamReader 클래스를 이용해서 읽을수 있습니다. 여기서 저는 XML형태로 저장해서 Linq To XML를 이용해서 내용을 읽어 봤어요. 파일 삭제는 IsolateStorageFile클래스의 DeleteFile 메소드를 이용해서 삭제가 가능해요.
IsolateStorageFile 맴버 확인은 MSDN를 확인해 보시면 잘나와 있어요.

Posted by happydong
, |