简体   繁体   English

将IsolatedStorageFileStream从Windows Phone 8.1转换为Windows 8.1

[英]Convert IsolatedStorageFileStream from Windows phone 8.1 to Windows 8.1

I wrote this code in windows phone application to read and write data from xml file and it is work fine. 我在Windows Phone应用程序中编写了此代码,以便从xml文件读取和写入数据,并且工作正常。 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 所以我想在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();

    }

You need to use the new API, to read a file 您需要使用新的API来读取文件

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

and to write 并写

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

The member DamithSL answer the question at code project site DamithSL成员在代码项目现场回答问题

http://www.codeproject.com/Questions/839861/Convert-IsolatedStorageFileStream-from-Windows-pho 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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM