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

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值