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

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

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

  1. c语言创造的文件保存路径_c语言怎么把变量写入文件路径

    1. c语言 如何将变量写入文件 比如写入 c盘下面的test.txt文件中. #include #include void main(void) { char achBuf[256]; memset ...

  2. fread,fwrite数据写磁盘流程|fflush--linux编程写文件注意问题(fwrite没有直接写入文件)

    目录 fread,fwrite数据写入磁盘的流程 fwrite,fflush fwrite和write的区别 fwrite,fflush-----linux编程写文件注意问题(fwrite没有直接写入 ...

  3. java数据写入文件方案,Java如何将字符串数据写入文件?

    package org.nhooo.example.commons.io; import org.apache.commons.io.FileUtils; import java.io.File; i ...

  4. java int数组写入文件中_Java程序将int数组写入文件

    这是我们的文件-FileWriter writer = new FileWriter("E:/demo.txt"); 现在,考虑一个整数数组-Integer arr[] = { 1 ...

  5. python生成文件夹并向文件夹写文件_python - 文件练习生成100个MAC地址写入文件

    需求: 生成100个MAC地址并写入文件中,MAC地址前6位(16进制)为01-AF-3B 解题思路: 要求生成这样格式的mac地址:01-AF-3B-xx-xx-xx 首先生成-xx格式,16进制组 ...

  6. java 写文件filewriter_使用FileReader和FileWriter读取写入文件内容

    1.Java的輸入与輸出 import java.io.DataInputStream; import java.io.IOException; public class InputAndOutput ...

  7. ftp服务器文件传输安全性创新点,利用FTP进行文件传输时的主要安全问题存在于...

    类型:文件管理大小:7.7M语言:中文 评分:10.0 标签: 立即下载 利用FTP进行文件传输时的主要安全问题存在于什么是大家比较关心的,很多小伙伴们不知道这个答案是什么,想要知道这个答案的小伙伴们 ...

  8. sqlite库——C实现,给sqlite数据库添加信息并把信息写入文件,删除日志和库中的日志信息

    一.功能 在开机启动时候,给sqlite3数据库内,添加 '固定' 信息的运行日志: 并把日志写入[.log]文件内: 日志信息的6个字段为: 时间time.类型type.主体subject.客体ob ...

  9. 69. (待补) (使用sqlite3)实现简单的管理系统 MVC 将链表作为内存数据模型,将sqlite3作为数据库,将终端作为交互界面。读数据库生成 链表,修改链表写入文件。...

    待补 转载于:https://www.cnblogs.com/ZhuLuoJiGongYuan/p/9571219.html

最新文章

  1. Vue 自定义权限指令
  2. 成功解决ImportError: Could not find 'msvcp140.dll'. TensorFlow requires that this DLL be installed in a
  3. hdu 1255(线段树求重叠面积)
  4. Android 内容提供者(Content provider)
  5. spy导入数据到oracle,运用SchemaSpy逆向工程制作数据库文档
  6. MySQL对字符集_对MySQL字符集的认识
  7. linux ntfs 用户权限,linux权限及ntfs文件系统权限的知识
  8. 使用开源ASR框架在Mono和.NET C#中进行语音识别
  9. Axure谷歌浏览器Chrome扩展程序下载及安装方法
  10. mysql修改密码、找回密码
  11. 安装版本swf文件转换其他视频格式工具(例:swf to mp4) ,转换后的视频无水印...
  12. 每天学点Linux:一
  13. 史上最全C/C++思维导图,B站疯传,快收藏!!(附配套学习视频)
  14. 深信服PHP,深信服终端检测响应平台 EDR 代码审计
  15. python手绘效果图_用Python做个海量小姐姐素描图
  16. 编程心得之逻辑判断的先后顺序
  17. Python中利用FFT(快速傅里叶变换)进行频谱分析
  18. 使用expdp(非本地)远程导出数据
  19. POJ 1700 经典过河问题(贪心)
  20. 用户画像建模(客户基本属性表,客户营销信息表)

热门文章

  1. leetcode 79.单词搜索 dfs
  2. OpenGL基础15:输入控制
  3. Codeforces Round #493 (Div. 2):D. Roman Digits
  4. bzoj 4260: Codechef REBXOR(01字典树)
  5. matlab fspecial
  6. matlab bwdist
  7. matlab2c使用c++实现matlab函数系列教程-rot90函数
  8. c#获取系统信息:CPU、内存、硬盘、用户、网络
  9. xilinx sdk退出Debug模式回到C开发布局
  10. xilinx sdk在Debug模式下根据地址在内存里观察值