简体   繁体   English

在Windows Phone(通用)应用程序中读写

[英]Read and Write in Windows Phone (Universal) application

Whether it is possible to write the text in the file that was created in the project directory. 是否可以在项目目录中创建的文件中写入文本。 Read the file I did it, but the record can somehow? 阅读我做过的文件,但是记录可以以某种方式显示吗? Or it is necessary to create a file via code? 还是有必要通过代码创建文件?

Yes it is very possible to write a text file via code. 是的,很可能通过代码编写文本文件。 First, create a model of how your file content will be saved eg 首先,创建一个如何保存文件内容的模型,例如

public class Person
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
}

Then this is your code for saving the above model to your local text file after serializing the object to a string. 然后,这是将对象序列化为字符串后将上述模型保存到本地文本文件的代码。 Use json.Net to do that 使用json.Net来做到这一点

private void AddPerson(Person person)
{        
        //save
        var mystream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("file.txt", CreationCollisionOption.ReplaceExisting);
        using (StreamWriter writer = new StreamWriter(mystream))
        {
            var x = JsonConvert.SerializeObject(person);
            writer.Write(x);
        }
}

This is very easy and fast. 这是非常容易和快速的。 Hope it helps 希望能帮助到你

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

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