当前位置:
首页 >
WPF:跨应用程序会话保持和还原应用程序范围的属性
发布时间:2023/11/27
50
豆豆
生活随笔
收集整理的这篇文章主要介绍了
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:跨应用程序会话保持和还原应用程序范围的属性的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 拍拍身上的灰尘是哪首歌啊?
- 下一篇: (转载)从无知到有知