goodniko
goodniko

Reputation: 163

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?

Upvotes: 0

Views: 39

Answers (1)

Timothy Macharia
Timothy Macharia

Reputation: 2926

Yes it is very possible to write a text file via code. First, create a model of how your file content will be saved e.g

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

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

Upvotes: 1

Related Questions