[文件、数据库、XML]window phone 利用StreamWriter写入文件问题

写了一段代码,内容是打开工程中的文本文件,写入数据,关键代码如下:

1   StreamResourceInfo filename = Application.GetResourceStream(new Uri("answers.txt", UriKind.Relative));
2              using (StreamWriter writer = new StreamWriter(filename.Stream))
3              {
4                  writer.WriteLine(inputStr);
5                  writer.Close();
6              }

其中,inputStr的是写入的数据。

运行时报错,说:Value does not fall within the expected range.

分析:Application.GetResourceStream拿到的是你xap安装目录下的文件,我不认为这个是可写的

解决:可能是我没有仔细研究下工程中的文件属性。
为了实现文件的可写,我利用了独立存储,想可以不断地往文本文件中添加新的数据,修改了一下。关键代码如下:

1    IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
2              IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Append, FileAccess.Write);
3              using (StreamWriter writer = new StreamWriter(fileStream))
4              {
5                  writer.Write(input);
6                  writer.Close();
7             }

这次ok了

下面附加wp中的隔离隔离储存空间的相关知识:

隔离存储空间:

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

重要的类:

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

windows phone下的隔离存储空间没有配额的限制

相关操作如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Net;
  5 using System.Windows;
  6 using System.Windows.Controls;
  7 using System.Windows.Documents;
  8 using System.Windows.Input;
  9 using System.Windows.Media;
 10 using System.Windows.Media.Animation;
 11 using System.Windows.Shapes;
 12 using Microsoft.Phone.Controls;
 13 using System.IO.IsolatedStorage;
 14 using System.IO;
 15 namespace IsolatedStorage
 16 {
 17     public partial class MainPage : PhoneApplicationPage
 18     {
 19         // Constructor
 20         public MainPage()
 21         {
 22             InitializeComponent();
 23         }
 24  
 25         private const string foldername = "temp1";
 26         private const string filename = foldername + "/address.txt";
 27         private const string settingname = "sname";
 28         /// <summary>
 29         /// 创建文件夹
 30         /// </summary>
 31         /// <param name="sender"></param>
 32         /// <param name="e"></param>
 33         private void button1_Click(object sender, RoutedEventArgs e)
 34         {
 35             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 36             {
 37                 file.CreateDirectory(foldername);
 38             }
 39         }
 40         /// <summary>
 41         /// 检查文件夹是否存在
 42         /// </summary>
 43         /// <param name="sender"></param>
 44         /// <param name="e"></param>
 45         private void button2_Click(object sender, RoutedEventArgs e)
 46         {
 47             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 48             {
 49                 if (file.DirectoryExists(foldername))
 50                 {
 51                     MessageBox.Show("已存在");
 52                 }
 53                 else
 54                 {
 55                     MessageBox.Show("不存在");
 56                 }
 57             }
 58         }
 59         /// <summary>
 60         /// 删除目录
 61         /// </summary>
 62         /// <param name="sender"></param>
 63         /// <param name="e"></param>
 64         private void button3_Click(object sender, RoutedEventArgs e)
 65         {
 66             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 67             {
 68                 file.DeleteDirectory(foldername);
 69             }
 70         }
 71         /// <summary>
 72         /// 创建文件
 73         /// </summary>
 74         /// <param name="sender"></param>
 75         /// <param name="e"></param>
 76         private void button4_Click(object sender, RoutedEventArgs e)
 77         {
 78             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 79             {
 80                 IsolatedStorageFileStream stream = file.CreateFile(filename);
 81                 stream.Close();
 82             }
 83         }
 84         /// <summary>
 85         /// 检查文件是否存在
 86         /// </summary>
 87         /// <param name="sender"></param>
 88         /// <param name="e"></param>
 89         private void button5_Click(object sender, RoutedEventArgs e)
 90         {
 91             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
 92             {
 93                 if (file.FileExists(filename))
 94                 {
 95                     MessageBox.Show("已存在" + filename);
 96                 }
 97                 else
 98                 {
 99                     MessageBox.Show("不存在");
100                 }
101             }
102         }
103         /// <summary>
104         /// 删除文件
105         /// </summary>
106         /// <param name="sender"></param>
107         /// <param name="e"></param>
108         private void button6_Click(object sender, RoutedEventArgs e)
109         {
110             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
111             {
112                 file.DeleteFile(filename);
113             }
114         }
115         /// <summary>
116         /// 向文件里增加内容
117         /// </summary>
118         /// <param name="sender"></param>
119         /// <param name="e"></param>
120         private void button7_Click(object sender, RoutedEventArgs e)
121         {
122             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
123             {
124                 using (IsolatedStorageFileStream stream = file.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
125                 {
126                     StreamWriter writer = new StreamWriter(stream);
127                     writer.WriteLine(textBox1.Text);
128                     writer.Close();
129                     textBox1.Text = "";
130                 }
131                 
132             }
133         }
134         /// <summary>
135         /// 读取文件内容
136         /// </summary>
137         /// <param name="sender"></param>
138         /// <param name="e"></param>
139         private void button8_Click(object sender, RoutedEventArgs e)
140         {
141             using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
142             {
143                 using (IsolatedStorageFileStream stream = file.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
144                 {
145                   using (StreamReader  reader=new StreamReader (stream))
146                   {
147                       textBox1.Text = reader.ReadToEnd();
148                   }
149                 }
150  
151             }
152         }
153         /// <summary>
154         /// 程序配置信息保存
155         /// </summary>
156         /// <param name="sender"></param>
157         /// <param name="e"></param>
158         private void button9_Click(object sender, RoutedEventArgs e)
159         {
160             IsolatedStorageSettings.ApplicationSettings[settingname] = textBox2.Text;
161             IsolatedStorageSettings.ApplicationSettings.Save();
162             textBox2.Text = "";
163         }
164         /// <summary>
165         /// 程序配置信息读取
166         /// </summary>
167         /// <param name="sender"></param>
168         /// <param name="e"></param>
169         private void button10_Click(object sender, RoutedEventArgs e)
170         {
171             if (IsolatedStorageSettings.ApplicationSettings.Contains(settingname))
172             {
173                 textBox2.Text = IsolatedStorageSettings.ApplicationSettings[settingname].ToString();
174             }
175         }
176  
177     }
178 }

原文:http://www.cnblogs.com/LittleFeiHu/archive/2012/02/28/2372311.html

 

转载于:https://www.cnblogs.com/hai-ping/articles/2731970.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值