zoukankan      html  css  js  c++  java
  • Windows Phone7隔离存储空间

    隔离存储空间:

    • 目录操作
    • 文件操作
    • 应用程序配置信息

     隔离存储空间的概念:所有文件IO操作被限制在隔离存储空间里面,在隔离存储空间里面可以增删改目录和文件,在隔离存储空间里面可以存储程序配置信息

    重要的类:

    • IsolatedStorageFile用于操控隔离存储空间里面的目录及文件,
    • IsolatedStorageFileStream用于读写操控隔离存储空间里面的流
    • IsolatedStorageFileSettings用于存储程序配置信息的Dictionary

    配额管理:

    • windows phone下的隔离存储空间没有配额的限制
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.IO.IsolatedStorage;
    using System.IO;
    namespace IsolatedStorage
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
    
            private const string foldername = "temp1";
            private const string filename = foldername + "/address.txt";
            private const string settingname = "sname";
            /// <summary>
            /// 创建文件夹
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.CreateDirectory(foldername);
                }
            }
            /// <summary>
            /// 检查文件夹是否存在
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.DirectoryExists(foldername))
                    {
                        MessageBox.Show("已存在");
                    }
                    else
                    {
                        MessageBox.Show("不存在");
                    }
                }
            }
            /// <summary>
            /// 删除目录
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button3_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.DeleteDirectory(foldername);
                }
            }
            /// <summary>
            /// 创建文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button4_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    IsolatedStorageFileStream stream = file.CreateFile(filename);
                    stream.Close();
                }
            }
            /// <summary>
            /// 检查文件是否存在
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button5_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.FileExists(filename))
                    {
                        MessageBox.Show("已存在" + filename);
                    }
                    else
                    {
                        MessageBox.Show("不存在");
                    }
                }
            }
            /// <summary>
            /// 删除文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button6_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.DeleteFile(filename);
                }
            }
            /// <summary>
            /// 向文件里增加内容
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button7_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        StreamWriter writer = new StreamWriter(stream);
                        writer.WriteLine(textBox1.Text);
                        writer.Close();
                        textBox1.Text = "";
                    }
                   
                }
            }
            /// <summary>
            /// 读取文件内容
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button8_Click(object sender, RoutedEventArgs e)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                      using (StreamReader  reader=new StreamReader (stream))
                      {
                          textBox1.Text = reader.ReadToEnd();
                      }
                    }
    
                }
            }
            /// <summary>
            /// 程序配置信息保存
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button9_Click(object sender, RoutedEventArgs e)
            {
                IsolatedStorageSettings.ApplicationSettings[settingname] = textBox2.Text;
                IsolatedStorageSettings.ApplicationSettings.Save();
                textBox2.Text = "";
            }
            /// <summary>
            /// 程序配置信息读取
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button10_Click(object sender, RoutedEventArgs e)
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(settingname))
                {
                    textBox2.Text = IsolatedStorageSettings.ApplicationSettings[settingname].ToString();
                }
            }
    
        }
    }
    

      

  • 相关阅读:
    leetcode 309. Best Time to Buy and Sell Stock with Cooldown
    leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee
    leetcode 32. Longest Valid Parentheses
    leetcode 224. Basic Calculator
    leetcode 540. Single Element in a Sorted Array
    leetcode 109. Convert Sorted List to Binary Search Tree
    leetcode 3. Longest Substring Without Repeating Characters
    leetcode 84. Largest Rectangle in Histogram
    leetcode 338. Counting Bits
    git教程之回到过去,版本对比
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/2372311.html
Copyright © 2011-2022 走看看