网络编程分类:ASP教程ASP.NET教程PHP教程JSP/Java教程CGI/Perl教程

silverlight如何在客户端写入文件

时间:2009-09-15 17:23:26 作者:编辑整理 来源:数字生活
核心提示:  Silverlight出于安全考虑,不允许在客户端读写客户端硬盘上的文件。但是它还是给程序员提供了变通的方法--独立存储(Isolated Storage)  Isolated Storage是Silverlight 2中提供的一个客户端安全的存储,它是

  下面是读取文件的代码:

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            // Open the file for reading
            stream = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read,isf);

            // Use the stream normally in a TextReader
            System.IO.TextReader reader = new System.IO.StreamReader(stream);
            string sLine = reader.ReadLine();
            this.tbTextsRead.Text = sLine;
            reader.Close(); // Close the reader
            stream.Close(); // Close the stream

  而Page.xaml.cs的代码全部如下:

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 System.IO.IsolatedStorage;
using System.Text;

namespace WriteLocalFileInSL
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        System.IO.Stream stream;

        private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            // Get the storage file for the application
            //Step1, you need to access an IsolatedStorageFile.  
            //This is done by calling IsolatedStorageFile.GetUserStoreForApplication() or IsolatedStorageFile.GetUserStoreForSite().  
            //There are some differences between the functions, but either will get you a private file system in which you can read and write files.

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            // Open&Create the file for writing
            //Step2, you need create or open your file almost as you would normally. 
            //Instead of using FileStream, you would use IsolatedStorageFileStream (which is derived from FileStream).
            //This means that once you have your stream, you can use it anywhere you would have normally used a normal FileStream.

            stream = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write, isf);

            // Use the stream normally in a TextWriter
            //Step3, you read or write as you normally would.

            System.IO.TextWriter  writer = new System.IO.StreamWriter(stream);
            writer.WriteLine(this.tbTexts.Text.ToString());
            writer.Close(); // Close the writer so data is flushed
            stream.Close(); // Close the stream too

     
        }

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            // Open the file for reading
            stream = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read,isf);

            // Use the stream normally in a TextReader
            System.IO.TextReader reader = new System.IO.StreamReader(stream);
            string sLine = reader.ReadLine();
            this.tbTextsRead.Text = sLine;
            reader.Close(); // Close the reader
            stream.Close(); // Close the stream
        }
    }
}

  按下F5,运行,效果如下:

  正在左侧文本框输入文本内容,然后按下写入文件按钮进行保存,效果如下:

  保存文件后,再按下读出文件按钮显示刚才保存的文件内容在右侧文本框内:

  我们还可以运用IsolatedStorage来操作Binary和Xml类型的文件。

  需要注意的是,用户可以通过Silverlight设置可以禁用这个功能

 3/3   |‹ ‹‹ 1 2 3
相关信息

推荐信息