将IsolatedStorageFileStream从Windows phone 8.1转换为Windows 8.1

本文关键字:Windows 转换 phone IsolatedStorageFileStream | 更新日期: 2023-09-27 18:29:39

我在windowsphone应用程序中编写了这段代码,用于从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 = 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();
    }

将IsolatedStorageFileStream从Windows phone 8.1转换为Windows 8.1

您需要使用新的API来读取文件

var file = await localFolder.GetFileAsync("dataFile.txt");
var data = await FileIO.ReadTextAsync(file);

并写入

var file = await localFolder.CreateFileAsync("dataFile.txt",        CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file , "data");

成员DamithSL在代码项目站点回答问题

http://www.codeproject.com/Questions/839861/Convert-IsolatedStorageFileStream-from-Windows-pho

public async Task Read(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);
        }
        XDocument loadedDataH = XDocument.Parse(text);
        var vPosting = from query in loadedDataH.Descendants("Row")
                       select new clssColPost(query);
        this.Clear();
        AddRange(vPosting);
    }

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

        IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
        IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
        using (Stream stream = await storageFile.OpenStreamForWriteAsync())
        {
            byte[] content = Encoding.UTF8.GetBytes(xml.ToString());
            await stream.WriteAsync(content, 0, content.Length);
        }
    }