我有一个关于使用Windows IoT在Raspberry PI上保存文件的问题 .
我想做什么:我有各种值(温度),我想记录 . 对我来说最简单的方法是编写一个包含所有值的简单txt文件 .
首先:甚至可以在SD卡上本地创建文件吗?因为我发现的代码示例只适用于“普通”Windows系统:
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的代码对我来说更有意义 . (另一方面根本不起作用) . 但即使电话代码是正确的,我也不知道如何访问内部存储 . 我已经尝试过IsolatedStorageExplorerTool,但它无法识别我的设备 . 可能是因为我的设备没有连接USB ......而且它不是手机 . 当我使用SSH时,我也找不到该目录 .
也许有人有个主意 . 提前感谢您的帮助!
2 回答
没关系,我找到了解决方案 . 这是Windows手机的代码 . 2.您必须使用Windows IoT核心观察程序 . 右键单击您的设备并打开网络共享 . 之后我找到了文本文件:\ c $ \ Users \ DefaultAccount \ AppData \ Local \ Packages \ LocalState \ TextFilesFolder
您可以将序列化用于有组织的存储而不是文本存储 .
我正在附加静态类文件,其中包含序列化和反序列化回原始对象的方法 .
Download Source Code
我们来看一个概括性的例子 . 假设您有Student和Mark课程,如下所示:
现在,为了序列化和反序列化,您将拥有以下带保存和加载功能的静态类:
现在,让我们看看如何存储学生的对象并从文件中检索它:
您可以序列化和反序列化任何类 . 要存储'Student'以外的类的对象,假设'MyClass',只需从函数的'T'参数替换Student类型,如下所示:
Download Source Code