加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – 在phone7中打开一个项目文件

发布时间:2020-12-14 04:02:14 所属栏目:Windows 来源:网络整理
导读:你好, 我在VisualStudio中有一个项目,它在根节点下面包含一个文件夹’xmlfiles’.这个文件夹包含一个文件’mensen.xml’,我尝试打开它… 但是,当我尝试打开该文件时,调试器会介入并抛出异常. 我试过了 ????????????if(File.Exists(@“/ xmlfiles / mensen.xm
你好,
我在VisualStudio中有一个项目,它在根节点下面包含一个文件夹’xmlfiles’.这个文件夹包含一个文件’mensen.xml’,我尝试打开它…

但是,当我尝试打开该文件时,调试器会介入并抛出异常.

我试过了

????????????if(File.Exists(@“/ xmlfiles / mensen.xml”))
????????????{
????????????????bool exists = true;
????????????}
以及:

FileStream fs = File.Open("/xmlfiles/mensen.xml",FileMode.Open);            
        TextReader textReader = new StreamReader(fs);
        kantinen = (meineKantinen)deserializer.Deserialize(textReader);
        textReader.Close();

Nothin正在工作:(.
如何在Phone7仿真器中打开本地文件?

解决方法

如果您只是打开它来阅读它,那么您可以执行以下操作(假设您已将文件的构建操作设置为资源):

System.IO.Stream myFileStream = Application.GetResourceStream(new Uri(@"/YOURASSEMBLY;component/xmlfiles/mensen.xml",UriKind.Relative)).Stream;

如果您尝试读取/写入此文件,则需要将其复制到隔离存储. (务必使用System.IO.IsolatedStorage添加)

您可以使用以下方法:

private void CopyFromContentToStorage(String fileName)
 {
   IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
   System.IO.Stream src = Application.GetResourceStream(new Uri(@"/YOURASSEMBLY;component/" + fileName,UriKind.Relative)).Stream;
   IsolatedStorageFileStream dest = new IsolatedStorageFileStream(fileName,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write,store);
   src.Position = 0;
   CopyStream(src,dest);
   dest.Flush();
   dest.Close();
   src.Close();
   dest.Dispose();
 }

 private static void CopyStream(System.IO.Stream input,IsolatedStorageFileStream output)
 {
   byte[] buffer = new byte[32768];
   long TempPos = input.Position;
   int readCount;
   do
   {
     readCount = input.Read(buffer,buffer.Length);
     if (readCount > 0) { output.Write(buffer,readCount); }
   } while (readCount > 0);
   input.Position = TempPos;
 }

在这两种情况下,请确保将文件设置为“资源”,然后使用程序集的名称替换YOURASSEMBLY部分.

使用上述方法,访问您的文件只需执行以下操作:

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (!store.FileExists(fileName))
{
  CopyFromContentToStorage(fileName);
}
store.OpenFile(fileName,System.IO.FileMode.Append);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读