【发布时间】:2017-02-02 17:49:06
【问题描述】:
我一直在努力寻找一种在 Win IoT 下的 Pi 上持久化 SQLite 数据库的方法,该数据库可以被不同的后台应用程序访问(不能同时访问)。
当我发现图书馆(音乐、图片、视频 - 但反常地不是文档,没有更多工作)时,我以为我已经找到了答案。我可以在一个应用程序中创建一个文本文件并将其写入图片库的默认文件夹。然后我可以用另一个应用程序读取文本文件。 File.Exists 返回真。宾果游戏(我想)!
但是,SQLite 不会在文件夹中创建数据库,也不会打开我复制到文件夹中的现有数据库。 SQLite.Net.SQLiteConnection 返回 SQLite 异常:“无法打开数据库文件:C:\Data\Users\DefaultAccount\Pictures\MyDb.db (CannotOpen)”——没有进一步的线索。
该文件夹似乎授予完全权限。请问大家有什么想法吗?
创建和编写文本文件:
using System;
using Windows.ApplicationModel.Background;
using System.IO;
using System.Diagnostics;
//*** NOTE: Pictures Library checked in Package.appxmanifest 'Capabilities'
namespace LibraryTest
{
public sealed class StartupTask : IBackgroundTask
{
private BackgroundTaskDeferral Deferral;
public async void Run (IBackgroundTaskInstance taskInstance)
{
Deferral = taskInstance.GetDeferral ();
var myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync
(Windows.Storage.KnownLibraryId.Pictures);
string path = myPictures.SaveFolder.Path;
Debug.WriteLine ($"'Pictures' Folder: {path}");
string newFilePath = Path.Combine (path, "TestTextFile.txt");
Debug.WriteLine ($"New File Path: {newFilePath}");
try {
using ( var stream = File.OpenWrite (newFilePath) ) {
using ( var writer = new StreamWriter (stream) ) {
writer.Write ("This is some test text.");
}
}
Debug.WriteLine ($"File created OK");
}
catch (Exception ex) { Debug.WriteLine ($"Exception: {ex.Message}"); }
}
}
}
制作:
'Pictures' Folder: C:\Data\Users\DefaultAccount\Pictures
New File Path: C:\Data\Users\DefaultAccount\Pictures\TestTextFile.txt
File created OK
阅读:
using System;
using Windows.ApplicationModel.Background;
using System.IO;
using System.Diagnostics;
//*** NOTE: Pictures Library checked in Package.appxmanifest 'Capabilities'
namespace ReadLibraryTest
{
public sealed class StartupTask : IBackgroundTask
{
private BackgroundTaskDeferral Deferral;
public async void Run (IBackgroundTaskInstance taskInstance)
{
Deferral = taskInstance.GetDeferral ();
var myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync
(Windows.Storage.KnownLibraryId.Pictures);
string path = myPictures.SaveFolder.Path;
Debug.WriteLine ($"'Pictures' Folder: {path}");
string newFilePath = Path.Combine (path, "TestTextFile.txt");
Debug.WriteLine ($"New File Path: {newFilePath}");
try {
using ( var stream = File.OpenRead (newFilePath) ) {
using ( var reader = new StreamReader (stream) ) {
string fileContents = reader.ReadLine ();
Debug.WriteLine ($"First line of file: '{fileContents}'");
}
}
Debug.WriteLine ($"File read OK");
}
catch ( Exception ex ) { Debug.WriteLine ($"Exception: {ex.Message}"); }
}
}
}
制作:
'Pictures' Folder: C:\Data\Users\DefaultAccount\Pictures
New File Path: C:\Data\Users\DefaultAccount\Pictures\TestTextFile.txt
First line of file: 'This is some test text.'
File read OK
【问题讨论】:
-
UWP 应用的路径“C:\Data\Users\DefaultAccount\Pictures\”访问限制导致的异常。您可以查看File access permissions。
-
感谢您的回复,丽塔。不过,奇怪的是,我能够使用标准 SYSTEM.IO 流方法在此文件夹中创建和读取文本文件。此外,我可以将一个文件复制到该文件夹中,然后从我的 PC 上的 Windows 资源管理器中打开该文件(它向作为 Pi 用户的我报告完全权限)。我不会想到 SQLite 需要比这些其他操作允许的读写权限更大的权限。
-
“我能够使用标准 SYSTEM.IO 流方法在此文件夹中创建和读取文本文件。” 你能显示这部分的代码吗?
-
丽塔,我已将代码添加到上面的原始问题中。新文件在 Windows 资源管理器中可见。
标签: c# iot raspberry-pi3 sqlite-net windowsiot