• 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]);
                    }
                }
            }
        }
    }

    本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

  • 相关阅读:
    Intellij IDEA 使用spring-boot-devtools无效解决办法一
    使用docker安装myql/redis等软件
    mybatis generator插件系列--分页插件
    mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)
    linux设置端口转发(一键设置)
    redis教程(The little redis book中文版)
    Redis 5种数据结构及其使用场景举例--STRING
    String中hashCode方法的线程安全
    java ShutdownHook介绍与使用
    ACM 模板库
  • 原文地址:https://www.cnblogs.com/zjypp/p/2319381.html
Copyright © 2020-2023  润新知