环境:Microsoft.Data.Sqlite.Core,SQLitePCLRaw.bundle_winsqlite3 2.0.1,Windows-10
,VS2019- ver16.3.6
以下代码在我的UWP
应用程序中起作用,可以在Downloads的Windows 10
文件夹中创建一个文件,也可以写入该文件。但是,如下面的second code block
中所示,当我通过创建SQLite
文件来做同样的事情时,例如,在sqliteSample.db
文件夹中单击Downloads
,然后尝试打开该数据库,则出现以下错误:
以下代码段适用于在“下载”文件夹中创建和写入文件:
StorageFile file = await Windows.Storage.DownloadsFolder.CreateFileAsync("sample.txt");
Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads
};
folderPicker.FileTypeFilter.Add(".txt");
StorageFolder oPickedFolder = await folderPicker.PickSingleFolderAsync();
if (oPickedFolder != null)
{
//Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", oPickedFolder);
string sSQLDbPath = Path.Combine(oPickedFolder.Path, "sample.txt");
Windows.Storage.StorageFile sampleFile = await oPickedFolder.GetFileAsync("sample.txt");
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "Swift as a shadow");
}
以下代码段在“下载”文件夹中成功创建了sqliteSample.db
文件,但是在尝试打开它时出现以下所示的错误:
备注:以下代码的第db.Open();
行出现以下错误。我已经在VS2019
的调试模式下验证了变量sSQLDbPath
具有正确的路径,当我将该路径手动复制/粘贴到Windows资源管理器时,可以在该文件的同一子文件夹中看到sqliteSample.db
文件下载的文件夹也具有通过上述代码创建的sample.txt
文件,并且可以正常运行。
SQLite错误14:“无法打开数据库文件”。
StorageFile file = await Windows.Storage.DownloadsFolder.CreateFileAsync("sqliteSample.db");
Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads
};
folderPicker.FileTypeFilter.Add(".db");
StorageFolder oPickedFolder = await folderPicker.PickSingleFolderAsync();
if (oPickedFolder != null)
{
//Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", oPickedFolder);
string sSQLDbPath = Path.Combine(oPickedFolder.Path, "sqliteSample.db");
using (SqliteConnection db = new SqliteConnection($"Filename={sSQLDbPath}"))
{
db.Open();
String tableCommand = "CREATE TABLE IF NOT " +
"EXISTS MyTable (Primary_Key INTEGER PRIMARY KEY, " +
"Text_Entry NVARCHAR(2048) NULL)";
SqliteCommand createTable = new SqliteCommand(tableCommand, db);
createTable.ExecuteReader();
}
}
来自File and folder permissions in the Downloads folder:
用户可以授予其他应用访问您的权限的权限从文件选择器中选择文件。您的应用也可以使用文件选择器以访问“下载”文件夹中的文件它没有创建。
无需具备在下载中创建或访问文件的功能。文件夹。
NOTE:This post也没有帮助。
简短的故事:SQLite无法看到下载文件夹。它只能写入应用程序的ApplicationData文件夹中的数据库。