微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight-IsolatedStorageFile来操作文件

IsolatedStorageFile的原理其实是在客户端允许的情况下载客户端创建文件文件会存在c盘的某一目录下。你可以用IsolatedStorageFile在客户端创建一个比较奇特的文件名,之后在c盘搜索你创建的文件你就会发现它把文件藏在哪里了。
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;

namespace IndicatorServiceSLDemo
{
     /// <summary>
     /// 操作客户端文件的类
     /// </summary>
     public  class FileOper
     {
         private static string endPointPath = @"endpoint.txt";
         private static string locatorPath = @"locator.txt";

         /// <summary>
         /// 创建文件
         /// </summary>
         /// <param name="path"></param>
        private static void createFile(string path)
         {
             if (!existFille(path))
             {
                 using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                 {
                     using (var stream = new IsolatedStorageFileStream(path,FileMode.Create,isoFile))
                     {
                         using (var writer = new StreamWriter(stream))
                         {
                             writer.Close();
                         }
                         stream.Close();
                     }
                 }
             }
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static bool existFille(string path)
        {
             using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 if (isoFile.FileExists(path))
                {
                    return true;
                }
                 return false;
             }
        }


        /// <summary>
        /// 为某文件添加信息
        /// </summary>
        /// <param name="path"></param>
        /// <param name="locator"></param>
        private static void addNewMessage(string path,string message)
        {
            createFile(path);
            using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = new IsolatedStorageFileStream(path,FileMode.Append,isoFile))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(message);
                        writer.Close();
                    }
                    stream.Close();
                }
            }
        }

        /// <summary>
        /// 获得某文件的全部信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static List<string> getMessages(String path)
        {
            List<string> locators = new List<string>();
            if (!existFille(path))
            {
                return locators;
            }
            using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = new IsolatedStorageFileStream(path,FileMode.Open,isoFile))
                {
                    using (var writer = new StreamReader(stream))
                    {
                        string locator;
                        while ((locator = writer.ReadLine()) != null)
                        {
                            locators.Add(locator);
                        }
                        writer.Close();
                    }
                    stream.Close();
                }
            }
            return locators;
        }


        /// <summary>
        /// 获得某文件的最新信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static string getNewMessage(String path)
        {
            List<string> messages = getMessages(path);
            if (messages.Count <= 0)
            {
                return null;
            }
            return messages[messages.Count - 1];
        }


        /// <summary>
        /// 添加一个locator
        /// </summary>
        /// <param name="locator"></param>
        public static void addNewLocator(string locator)
        {
            addNewMessage(locatorPath,locator);
        }

        /// <summary>
        /// 添加一个endpoint
        /// </summary>
        /// <param name="endpoint"></param>
        public static void addNewEndPoint(string endpoint)
        {
             addNewMessage(endPointPath,endpoint);
        }

        /// <summary>
        /// 获取最新的locator
        /// </summary>
        /// <returns></returns>
        public static string getNewLastLocator()
        {
            return getNewMessage(locatorPath);
        }

        /// <summary>
        /// 获取最新的endpoint
        /// </summary>
        /// <returns></returns>
        public static string getNewEndPoint()
        {
            return getNewMessage(endPointPath);
        }
       
     }

 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐