序幕:
我正在为 UWP 编写 SQLite GUI 客户端。我使用Microsoft.Data.Sqlite
C# 的 SQLite API 库。我还使用重定向表来打开我在 Microsoft Store 中发布的沙盒应用程序中的数据库。重定向表替换CreateFileW
为CreateFileFromAppW
调用和类似的。
问题:
用户具有File
->Save as
功能。当用户创建一个新的数据库文件时,会在应用程序本地目录中创建一个。接下来当用户保存他/她的数据库时,我需要移动这个文件。我使用StorageFile
API 因为我不能在沙盒应用程序中使用任何其他文件 API。所以我打电话:
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("SQLite3 Database", new List<string>() { ".sqlite", ".db" });
savePicker.SuggestedFileName = "Database";
var file = await savePicker.PickSaveFileAsync();
if(null != file)
{
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
sqliteConnection.Close(); // it is important cause if I skip this moving a file will fail
var localFolder = ApplicationData.Current.LocalFolder;
var currentStorageFile = await localFolder.GetFileAsync(App.UnsavedDatabaseFileName); // here I obtain StorageFile for opened database
await currentStorageFile.MoveAndReplaceAsync(file); // here I move a file
sqliteConnection = new SqliteConnection("Data Source=" + file.Path);
sqliteConnection.Open(); // this line fails with error 14: cannot open database file
}
我还尝试跳过关闭并重新打开连接->然后移动文件失败。如果我FileOpenPicker
在两者之间调用await currentStorageFile.MoveAndReplaceAsync(file);
,sqliteConnection = new SqliteConnection("Data Source=" + file.Path);
那么一切都会正常工作,但是在文件保存选择器之后立即显示文件打开选择器是非常糟糕的用户体验。我知道沙盒应用程序仅在用户手动选择文件后才授予文件访问权限。但看起来这FileSavePicker
并没有像我一样给我许可FileOpenPicker
。我找不到任何有关它的信息。
结语:
这是应用程序https://sqliteman.dev。请随时批评,因为它使我的应用程序变得更好。