WPF:跨应用程序会话保持和还原应用程序范围的属性
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                WPF:跨应用程序会话保持和还原应用程序范围的属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            所謂的wpf夸應用程序員會話保持和還原。其實就是將多個應用程序都用的資源保存到一個獨立的文件存儲系統中。這個應用程序退出的時候將數據寫入文件中,其他應用程序使用的時候可以去讀取這個文件
這個地方用到了System.IO.IsolatedStorage。這個方法只是為了避免讀寫文件操作的時候可能遇到的權限問題。其他的內容就是簡單的文件讀寫了
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SDKSample.App" StartupUri="MainWindow.xaml" Startup="App_Startup" Exit="App_Exit"></Application>
//后臺cs代碼
using System.Windows; // Application, StartupEventArgs
using System.IO; // StreamReader, FileMode
using System.IO.IsolatedStorage; // IsolatedStorageFile, IsolatedStorageFileStream
namespace SDKSample
{
??? public partial class App : Application
??? {
??????? string filename = "App.txt";
private void App_Startup(object sender, StartupEventArgs e)
{
??? // Restore application-scope property from isolated storage
??? IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
??? try
??? {
??????? using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Open, storage))
??????? using (StreamReader reader = new StreamReader(stream))
??????? {
??????????? // Restore each application-scope property individually
??????????? while (!reader.EndOfStream)
??????????? {
??????????????? string[] keyValue = reader.ReadLine().Split(new char[] {','});
??????????????? this.Properties[keyValue[0]] = keyValue[1];
??????????? }
??????? }
??? }
??? catch (FileNotFoundException ex)
??? {
??????? // Handle when file is not found in isolated storage:
??????? // * When the first application session
??????? // * When file has been deleted
??????????? }
??????? }
??????? private void App_Exit(object sender, ExitEventArgs e)
??????? {
??????????? // Persist application-scope property to isolated storage
??????????? IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
??????????? using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Create, storage))
??????????? using (StreamWriter writer = new StreamWriter(stream))
??????????? {
??????????????? // Persist each application-scope property individually
??????????????? foreach (string key in this.Properties.Keys)
??????????????? {
??????????????????? writer.WriteLine("{0},{1}", key, this.Properties[key]);
??????????????? }
??????????? }
??????? }
??? }
}
                        
                        
                        這個地方用到了System.IO.IsolatedStorage。這個方法只是為了避免讀寫文件操作的時候可能遇到的權限問題。其他的內容就是簡單的文件讀寫了
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SDKSample.App" StartupUri="MainWindow.xaml" Startup="App_Startup" Exit="App_Exit"></Application>
//后臺cs代碼
using System.Windows; // Application, StartupEventArgs
using System.IO; // StreamReader, FileMode
using System.IO.IsolatedStorage; // IsolatedStorageFile, IsolatedStorageFileStream
namespace SDKSample
{
??? public partial class App : Application
??? {
??????? string filename = "App.txt";
private void App_Startup(object sender, StartupEventArgs e)
{
??? // Restore application-scope property from isolated storage
??? IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
??? try
??? {
??????? using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Open, storage))
??????? using (StreamReader reader = new StreamReader(stream))
??????? {
??????????? // Restore each application-scope property individually
??????????? while (!reader.EndOfStream)
??????????? {
??????????????? string[] keyValue = reader.ReadLine().Split(new char[] {','});
??????????????? this.Properties[keyValue[0]] = keyValue[1];
??????????? }
??????? }
??? }
??? catch (FileNotFoundException ex)
??? {
??????? // Handle when file is not found in isolated storage:
??????? // * When the first application session
??????? // * When file has been deleted
??????????? }
??????? }
??????? private void App_Exit(object sender, ExitEventArgs e)
??????? {
??????????? // Persist application-scope property to isolated storage
??????????? IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
??????????? using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Create, storage))
??????????? using (StreamWriter writer = new StreamWriter(stream))
??????????? {
??????????????? // Persist each application-scope property individually
??????????????? foreach (string key in this.Properties.Keys)
??????????????? {
??????????????????? writer.WriteLine("{0},{1}", key, this.Properties[key]);
??????????????? }
??????????? }
??????? }
??? }
}
本文使用Blog_Backup未注冊版本導出,請到soft.pt42.com注冊。
轉載于:https://www.cnblogs.com/zjypp/archive/2010/02/23/2319381.html
總結
以上是生活随笔為你收集整理的WPF:跨应用程序会话保持和还原应用程序范围的属性的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 拍拍身上的灰尘是哪首歌啊?
 - 下一篇: 求一个qq头像和网名一套女生版。