Saving files on Raspberry PI with Windows IoT

混江龙づ霸主 提交于 2019-12-04 17:23:06

Nevermind, I found the solution. 1. It's the code for Windows phone. 2. You have to use the Windows IoT Core Watcher. Right click on your device and open the network share. Afterwards I found the text file at: \\c$\Users\DefaultAccount\AppData\Local\Packages\\LocalState\TextFilesFolder

You can use serialization for organized storage than textual storage.

I'm attaching static class file that contains method to serialize and de-serialize back to the original object.

Download Source Code

Let's take a generalize example. Assume, you have Student and Mark class as below:

/// <summary>
/// Provides structure for 'Student' entity
/// </summary>
/// 'DataContract' attribute is necessary to serialize object of following class. By removing 'DataContract' attribute, the following class 'Student' will no longer be serialized
[DataContract]
public class Student
{
    [DataMember]
    public ushort Id { get; set; }

    [DataMember]
    public string UserName { get; set; }

    /// <summary>
    /// Password has been marked as non-serializable by removing 'DataContract'
    /// </summary>
    // [DataMember] // Password will not be serialized. Uncomment this line to serialize password
    public string Password { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public List<Mark> Marks { get; set; }
}

[DataContract]
public class Mark
{
    [DataMember]
    public string Subject { get; set; }

    [DataMember]
    public short Percentage { get; set; }
}

Make sure to attribute '[DataContract]' on calss and '[DataMember]' on data member to serialize them else they will be ignore while serializing object


Now, to serialize and deserialize, you will have following static class with Save and Load function:

/// <summary>
/// Provides functions to save and load single object as well as List of 'T' using serialization
/// </summary>
/// <typeparam name="T">Type parameter to be serialize</typeparam>
public static class SerializableStorage<T> where T : new()
{
    public static async void Save(string FileName, T _Data)
    {
        MemoryStream _MemoryStream = new MemoryStream();
        DataContractSerializer Serializer = new DataContractSerializer(typeof(T));
        Serializer.WriteObject(_MemoryStream, _Data);

        Task.WaitAll();

        StorageFile _File = await ApplicationData.Current.LocalFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);

        using (Stream fileStream = await _File.OpenStreamForWriteAsync())
        {
            _MemoryStream.Seek(0, SeekOrigin.Begin);
            await _MemoryStream.CopyToAsync(fileStream);
            await fileStream.FlushAsync();
            fileStream.Dispose();
        }
    }

    public static async Task<T> Load(string FileName)
    {
        StorageFolder _Folder = ApplicationData.Current.LocalFolder;
        StorageFile _File;
        T Result;

        try
        {
            Task.WaitAll();
            _File = await _Folder.GetFileAsync(FileName);

            using (Stream stream = await _File.OpenStreamForReadAsync())
            {
                DataContractSerializer Serializer = new DataContractSerializer(typeof(T));

                Result = (T)Serializer.ReadObject(stream);

            }
            return Result;
        }
        catch (Exception ex)
        {
            return new T();
        }
    }
}


Now, let's see how to store object of a Student and retrieve it back from file:

/* Create an object of Student class to store */
Student s1 = new Student();
s1.Id = 1;
s1.UserName = "Student1";
s1.Password = "Student123";
s1.FirstName = "Abc";
s1.LastName = "Xyz";
s1.Marks = new List<Mark>();

/* Create list of Marks */
Mark m1 = new Mark();
m1.Subject = "Computer";
m1.Percentage = 89;

Mark m2 = new Mark();
m2.Subject = "Physics";
m2.Percentage = 92;

/* Add marks into Student object */
s1.Marks.Add(m1);
s1.Marks.Add(m2);

/* Store Student object 's1' into file 'MyFile1.dat' */
SerializableStorage<Student>.Save("MyFile1.dat", s1);

/* Load stored student object from 'MyFile1.dat' */
Student s2 = await SerializableStorage<Student>.Load("MyFile1.dat");


You can serialize and de-serialize any class. To store object of a class other than 'Student', suppose 'MyClass', just replace Student type from 'T' parameter of the function as below:

/* Store MyClass object 's1' into file 'MyFile1.dat' */
SerializableStorage<MyClass>.Save("MyFile1.dat", s1);

/* Load stored MyClass object from 'MyFile1.dat' */
MyClass s2 = await SerializableStorage<MyClass>.Load("MyFile1.dat");

NOTE: 'MyFile1.dat' will be stored in 'ApplicationData.Current.LocalFolder'. This code is tested on Windows 10 IoT Core (10.0.10586.0) and can work on any UWP app.

Download Source Code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!