C# 使用Windows IoT在Raspberry PI上保存文件
我有一个关于用Windows IoT在Raspberry PI上保存文件的问题 我想做的是: 我有各种要记录的值(温度)。对我来说,最简单的方法是,编写一个包含所有值的简单txt文件 首先:甚至可以在SD卡上本地创建文件吗?因为我发现的代码示例仅适用于“普通”Windows系统:C# 使用Windows IoT在Raspberry PI上保存文件,c#,raspberry-pi,windowsiot,C#,Raspberry Pi,Windowsiot,我有一个关于用Windows IoT在Raspberry PI上保存文件的问题 我想做的是: 我有各种要记录的值(温度)。对我来说,最简单的方法是,编写一个包含所有值的简单txt文件 首先:甚至可以在SD卡上本地创建文件吗?因为我发现的代码示例仅适用于“普通”Windows系统: if (!File.Exists(path)) { // Create a file to write to. string createText = "Hell
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
}
或在Windows Phone上:
public void createdirectory()
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
myIsolatedStorage.CreateDirectory("TextFilesFolder");
filename = "TextFilesFolder\\Samplefile.txt";
Create_new_file();
}
public void Create_new_file()
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!myIsolatedStorage.FileExists(filename))
{
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
string someTextData = "This is a test!";
writeFile.WriteLine(someTextData);
// writeFile.Close();
}
}
Windows Phone的代码对我来说更有意义。(另一个根本不起作用)。但即使电话号码是正确的,我也不知道如何访问内部存储器。我尝试过隔离存储库ExplorerTool,但它无法识别我的设备。可能是因为我的设备没有连接USB。。。这不是电话。当我使用SSH时,我也找不到目录
也许有人有主意。提前谢谢你的帮助 不管怎样,我找到了解决办法。 1.这是Windows phone的代码。 2.您必须使用Windows IoT Core Watcher。右键单击设备并打开网络共享。之后,我在以下位置找到了文本文件:
\\c$\Users\DefaultAccount\AppData\Local\Packages\\LocalState\TextFilesFolder对于有组织的存储,可以使用序列化,而不是文本存储 我正在将包含要序列化和反序列化的方法的静态类文件附加回原始对象 让我们举一个概括的例子。假设您的学生和分数如下所示:
/// <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; }
}
/* 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");
//
///提供“学生”实体的结构
///
///“DataContract”属性是序列化以下类的对象所必需的。通过删除“DataContract”属性,将不再序列化以下类“Student”
[数据合同]
公立班学生
{
[数据成员]
公共ushort Id{get;set;}
[数据成员]
公共字符串用户名{get;set;}
///
///已通过删除“DataContract”将密码标记为不可序列化
///
//[DataMember]//密码不会被序列化。请取消对此行的注释以序列化密码
公共字符串密码{get;set;}
[数据成员]
公共字符串名{get;set;}
[数据成员]
公共字符串LastName{get;set;}
[数据成员]
公共列表标记{get;set;}
}
[数据合同]
公共等级标志
{
[数据成员]
公共字符串主题{get;set;}
[数据成员]
公共短百分比{get;set;}
}
确保将calss上的“[DataContract]”属性和数据成员上的“[DataMember]”属性序列化,否则序列化对象时将忽略它们
现在,要序列化和反序列化,您将拥有以下带有保存和加载功能的静态类:
/// <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();
}
}
}
//
///提供使用序列化保存和加载单个对象以及“T”列表的函数
///
///要序列化的类型参数
公共静态类SerializableStorage,其中T:new()
{
公共静态异步void保存(字符串文件名,T_数据)
{
MemoryStream_MemoryStream=新的MemoryStream();
DataContractSerializer Serializer=新的DataContractSerializer(typeof(T));
Serializer.WriteObject(_MemoryStream,_Data);
Task.WaitAll();
StorageFile\u File=wait ApplicationData.Current.LocalFolder.CreateFileAsync(文件名,CreationCollisionOption.ReplaceExisting);
使用(Stream fileStream=await_File.OpenStreamForWriteAsync())
{
_MemoryStream.Seek(0,SeekOrigin.Begin);
wait_MemoryStream.CopyToAsync(fileStream);
等待fileStream.FlushAsync();
Dispose();
}
}
公共静态异步任务加载(字符串文件名)
{
StorageFolder\u Folder=ApplicationData.Current.LocalFolder;
StorageFile\u文件;
T结果;
尝试
{
Task.WaitAll();
_File=await\u Folder.GetFileAsync(文件名);
使用(Stream-Stream=await_File.OpenStreamForReadAsync())
{
DataContractSerializer Serializer=新的DataContractSerializer(typeof(T));
结果=(T)Serializer.ReadObject(流);
}
返回结果;
}
捕获(例外情况除外)
{
返回新的T();
}
}
}
现在,让我们看看如何存储学生的对象并从文件中检索它:
/* 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");
/*创建要存储的Student类对象*/
学生s1=新学生();
s1.Id=1;
s1.UserName=“Student1”;
s1.Password=“Student123”;
s1.FirstName=“Abc”;
s1.LastName=“Xyz”;
s1.标记=新列表();
/*创建标记列表*/
标记m1=新标记();
m1.Subject=“计算机”;
m1.百分比=89;
标记m2=新标记();
m2.Subject=“物理学”;
m2.百分比=92;
/*将分数添加到学生对象中*/
s1.标记。添加(m1);
s1.标记。添加(m2);
/*将学生对象“s1”存储到文件“MyFile1.dat”中*/
SerializableStorage.Save(“MyFile1.dat”,s1);
/*从“MyFile1.dat”加载存储的学生对象*/
学生s2=等待SerializableStorage.Load(“MyFile1.dat”);
您可以序列化和反序列化任何类。若要存储“Student”以外的类的对象,假设“MyClass”,只需从函数的“T”参数中替换Student类型,如下所示:
/// <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; }
}
/* 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");
/*将MyClass对象“s1”存储到文件“MyFile1.dat”中*/
SerializableStorage.Save(“MyFile1.dat”,s1);
/*从“MyFile1.dat”加载存储的MyClass对象*/
MyClass s2=等待SerializableStorage.Load(“MyFile1.dat”);
注意:“MyFile1.dat”将存储在“”。此代码在Windows 10 IoT Core(10.0.10586.0)上测试,可以在任何UWP应用程序上运行
使用Raspbian/bash有什么问题?