如何编写在Windows 8中使用的StreamWriter文件? [英] How write a file using StreamWriter in Windows 8?

查看:181
本文介绍了如何编写在Windows 8中使用的StreamWriter文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows中创建-8 的StreamWriter 对象时遇到了麻烦,平时我只是创建只是传递一个字符串作为参数的实例,但在Windows 8中我得到指示,它应该免费获赠流错误,但我注意到,流是一个抽象类,是否有人知道如何将是code写一个xml文件?顺便说一句,我使用的.xml因为我想保存序列化对象,或有没有人知道如何保存到一个文件序列化对象在Windows 8?

I'm having trouble when creating a StreamWriter object in windows-8, usually I just create an instance just passing a string as a parameter, but in Windows 8 I get an error that indicates that it should recieve a Stream, but I noticed that Stream is an abstract class, Does anybody knows how will be the code to write an xml file?, BTW I'm using .xml because I want to save the serialized object, or does anyone knows how to save to a file a serialized object in Windows 8?.

任何想法?

目前使用的是Windows 8消费者preVIEW

Currently using Windows 8 Consumer Preview

code:

的StreamWriter SW =新的StreamWriter(person.xml);

错误:

的最好重载方法匹配'System.IO.StreamWriter.StreamWriter(的System.IO.Stream)'有一些无效参数

推荐答案

相反的StreamWriter,你会使用这样的:

Instead of StreamWriter you would use something like this:

StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync();

using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
    {
        using (DataWriter dataWriter = new DataWriter(outputStream))
        {
            //TODO: Replace "Bytes" with the type you want to write.
            dataWriter.WriteBytes(bytes);
            await dataWriter.StoreAsync();
            dataWriter.DetachStream();
        }

        await outputStream.FlushAsync();
    }
}

您可以看看<一个href=\"http://winrtxamltoolkit.$c$cplex.com/SourceControl/changeset/view/12598#235918\">StringIOExtensions类样品使用 WinRTXamlToolkit 库。

You can look at the StringIOExtensions class in the WinRTXamlToolkit library for sample use.

编辑*

虽然上述所有应该努力 - 他们以前写的<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.fileio.aspx\"><$c$c>FileIO类WinRT中,从而简化了大多数常见的情况是,上述解决方案解决了,因为你可以现在只需要调用<一面世href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.fileio.writetextasync.aspx\"><$c$c>await FileIO.WriteTextAsync(文件,内容) 写入文本文件中,也有类似的方法来读,写或追加字符串,字节字符串列表或 IBuffers

While all the above should work - they were written before the FileIO class became available in WinRT, which simplifies most of the common scenarios that the above solution solves since you can now just call await FileIO.WriteTextAsync(file, contents) to write text into file and there are also similar methods to read, write or append strings, bytes, lists of strings or IBuffers.

这篇关于如何编写在Windows 8中使用的StreamWriter文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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