将IsolatedStorageFileStream从Windows Phone 8.1转换为Windows 8.1 [英] Convert IsolatedStorageFileStream from Windows phone 8.1 to Windows 8.1

查看:68
本文介绍了将IsolatedStorageFileStream从Windows Phone 8.1转换为Windows 8.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在windows phone应用程序中编写了这段代码,用于从xml文件读取和写入数据,并且工作正常。

所以我想在Windows 8.1应用程序中使用它但是它不起作用,我怎么样将其转换为与Windows 8.1兼容



 public void Read(string strXMLFile)
{
IsolatedStorageFile isfData = IsolatedStorageFile .GetUserStoreForApplication();
XDocument doc = null;
IsolatedStorageFileStream isfStream = null;
if(isfData.FileExists(strXMLFile))
{
isfStream = new IsolatedStorageFileStream(strXMLFile,FileMode.Open,isfData);
doc = XDocument.Load(isfStream);
isfStream.Close();
}
else
{
doc = XDocument.Load(strXMLFile);
isfStream = new IsolatedStorageFileStream(strXMLFile,FileMode.CreateNew,isfData);
doc.Save(isfStream);
isfStream.Close();
}

var vData =来自doc.Descendants(Row)中的s选择新的ColData(s);
}


public void Write(string strXMLFile)
{
XElement xml = new XElement(Tabel,
from p in这个
选择p.Information);

IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strXMLFile,FileMode.Open,IsolatedStorageFile.GetUserStoreForApplication());
xml.Save(isfStream);
isfStream.Close();

}

解决方案

检查以下方法,如果你在windows中有设置开发环境8 PC,你最好逐行调试,检查出了什么问题。

写一个文件:

  public   async 任务WriteFile( string  fileName, string  text)
{
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName,CreationCollisionOption.ReplaceExisting);

使用(Stream stream = await storageFile.OpenStreamForWriteAsync())
{
byte [] content = Encoding.UTF8.GetBytes(text);
await stream.WriteAsync(content, 0 ,content.Length);
}
}



读取文件:

  public   async 任务< string> ReadFile( string  fileName)
{
string text;
IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);

IRandomAccessStream accessStream = await storageFile.OpenReadAsync();

使用(Stream stream = accessStream.AsStreamForRead(( int )accessStream。大小))
{
byte [] content = new 字节 [stream.Length];
await stream.ReadAsync(content, 0 ,( int )stream.Length);

text = Encoding.UTF8.GetString(content, 0 ,content.Length);
}

return text;
} < / string >



参考: Windows Phone 8共享核心与Windows 8 - 文件IO [ ^ ]


I wrote this code in windows phone application to read and write data from xml file and it is work fine.
so i want to use it at windows 8.1 application but it's not work, how i convert it to be compatible with windows 8.1

public void Read(string strXMLFile)
       {
           IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
           XDocument doc = null;
           IsolatedStorageFileStream isfStream = null;
           if (isfData.FileExists(strXMLFile))
           {
               isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData);
               doc = XDocument.Load(isfStream);
               isfStream.Close();
           }
           else
           {
               doc = XDocument.Load(strXMLFile);
               isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData);
               doc.Save(isfStream);
               isfStream.Close();
           }

           var vData = from s in doc.Descendants("Row") select new ColData(s);
       }


       public void Write(string strXMLFile)
       {
           XElement xml = new XElement("Tabel",
                           from p in this
                           select p.Information);

           IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication());
           xml.Save(isfStream);
           isfStream.Close();

       }

解决方案

check with below methods, if you have setup development environment in windows 8 PC, you better debug line by line and check what is going wrong.
To write a file:

public async Task WriteFile(string fileName, string text)
{
    IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

    IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

    using (Stream stream = await storageFile.OpenStreamForWriteAsync())
    {
        byte[] content = Encoding.UTF8.GetBytes(text);
        await stream.WriteAsync(content, 0, content.Length);
    }
}


To read a file:

public async Task<string> ReadFile(string fileName)
{
    string text;
    IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

    IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);

    IRandomAccessStream accessStream = await storageFile.OpenReadAsync();

    using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
    {
        byte[] content = new byte[stream.Length];
        await stream.ReadAsync(content, 0, (int) stream.Length);

        text = Encoding.UTF8.GetString(content, 0, content.Length);
    }

    return text;
}</string>


Ref : Windows Phone 8 Shared Core With Windows 8 – File IO[^]


这篇关于将IsolatedStorageFileStream从Windows Phone 8.1转换为Windows 8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆