2

below is my code to open a database in a user-selected folder

SqliteConnection db;
public async Task<string> openDatabase(String databaseFilename)
{

    db = new SqliteConnection($"Filename={databaseFilename}");
    await db.OpenAsync();
    return null;
}
//...
 


var folderPicker = new FolderPicker();//must-use broker in UWP
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
string encryptedUserSelectedFolderString = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder);


//...

StorageFolder fd = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(encryptedUserSelectedFolderString);//user-selected folder
var t = await fd.TryGetItemAsync("data.data");//sqlite in the folder
if (t != null)  
{
   db.openDatabase(Path.Combine(fd.Path,"data.data"));//this line causes app freezing
}

it reports the error:

unable to open the database file

...but the same code:

StorageFolder fd = await StorageFolder.GetFolderFromPathAsync(ApplicationData.Current.LocalFolder.Path);  
var t = await fd.TryGetItemAsync("data.data");
db.openDatabase(Path.Combine(fd.Path, "data.data"));//open database correctly

...can open the database in ApplicationData.Current.LocalFolder without any problem

Just wondering if there is a special requirement to open the database in the user-selected folder?

--------------------------------------------------updated question

my code to open a png file in the sub folder of the user-selected folder

Task<StorageFolder> folder = folderForBookmark(encryptedUserSelectedFolderString);            
StorageFolder bbb =await folder;            
StorageFile aaa = await  bbb.GetFileAsync(  "subfolder\\1.png");            
IRandomAccessStream fileStream = await aaa.OpenAsync(Windows.Storage.FileAccessMode.Read);            
//*create Bitmap from filestream            
BitmapImage bmpImage = new BitmapImage();            
bmpImage.SetSource(fileStream);            
//load bitmap into image-control            
aImage.Source = bmpImage;

it works! it can load the image file

arachide
  • 8,006
  • 18
  • 71
  • 134
  • I have used a broker to select the folder, so I can copy or rename the database file in the folder, but still can not open it – arachide Sep 21 '20 at 04:37
  • UWP apps are sandboxed and any attempt to open files outside of their claim will be denied. You need the _broad filesystem_ right if you want to access any location. Even if `encryptedUserSelectedFolderString` points to `My Documents` you need the **Documents** right. If the _"bridge"_ is native non-UWP code it probably doesn't prove much that it can copy the file if the destination folder is outside your UWP domain –  Sep 21 '20 at 04:40
  • but why I can rename the database file? – arachide Sep 21 '20 at 04:44
  • In the Debugger, what is the value of `databaseFilename` inside the call to `openDatabase()`? –  Sep 21 '20 at 04:46
  • c:\\users\\mypc\\Desktop\\tem\\data.data and c:\\users\\mypc\\Desktop\\tem is the user-selected folder – arachide Sep 21 '20 at 05:55
  • before this case, I think I can process any file under the user-selected folder c:\\users\\mypc\\Desktop\\tem, include copy rename and open them. I am sure that copy files to this folder and rename the files in the folder have no any problem. – arachide Sep 21 '20 at 05:59
  • I see the path involves the user's **Desktop** which requires [`broadFileSystemAccess`](https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations). I don't understand why you keep bringing up file rename. UWP apps are not like any other Windows app –  Sep 21 '20 at 06:16
  • I confused by UWP, while on macOS, I have all rights to open the files in the user-selected folder and its sub folder – arachide Sep 21 '20 at 09:31
  • I think you need to read up on UWP –  Sep 21 '20 at 09:35
  • I try to open the image files in the folder without any problem, it looks like only the database file can not be opened, so I guess the line db = new SqliteConnection($"Filename={databaseFilename}"); causes this happen – arachide Sep 22 '20 at 16:36
  • @MickyD please refer to my updated question – arachide Sep 22 '20 at 16:53

0 Answers0