■ Application 클래스의 Startup/Exit 이벤트를 사용해 애플리케이션 시작/종료시 처리하는 방법을 보여준다.
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 9 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" Startup="Application_Startup" Exit="Application_Exit"> </Application> |
▶ MainApplication.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
using System.IO; using System.IO.IsolatedStorage; using System.Windows; namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일명 /// </summary> private string fileName = "MainApplication.setting"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainApplication() /// <summary> /// 생성자 /// </summary> public MainApplication() { Properties["COUNT"] = 100; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 애플리케이션 시작시 처리하기 - Application_Startup(sender, e) /// <summary> /// 애플리케이션 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Application_Startup(object sender, StartupEventArgs e) { // C:\Users\계정명\AppData\Local\IsolatedStorage IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForDomain(); try { using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream(this.fileName, FileMode.Open, storageFile)) { using(StreamReader reader = new StreamReader(stream)) { while(!reader.EndOfStream) { string[] keyValueArray = reader.ReadLine().Split(new char[] { ',' }); Properties[keyValueArray[0]] = keyValueArray[1]; } } } } catch(FileNotFoundException) { } } #endregion #region 애플리케이션 종료시 처리하기 - Application_Exit(sender, e) /// <summary> /// 애플리케이션 종료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Application_Exit(object sender, ExitEventArgs e) { IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForDomain(); using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream(this.fileName, FileMode.Create, storageFile)) { using(StreamWriter writer = new StreamWriter(stream)) { foreach(string key in this.Properties.Keys) { writer.WriteLine("{0},{1}", key, Properties[key]); } } } } #endregion } } |