I am having issues getting this to run in a UWP desktop app.
````
private string Filename { get { return "credentials"; } }
private string DBName { get { return @"OvalCF.db"; } }
public IEnumerable
{
using( var db = new LiteDatabase( this.DBName ) )
{
return db.GetCollection
.FindAll().ToList();
}
}
when calling this method I keep getting the following exception:
System.UnauthorizedAccessException occurred
HResult=0x80070005
Message=Access to the path 'C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\bin\x86\Debug\AppX\OvalCF.db' is denied.
Source=
StackTrace:
at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at LiteDB.FileDiskService.CreateFileStream(String path, FileMode mode, FileAccess access, FileShare share)
at LiteDB.FileDiskService.Initialize(Logger log, String password)
at LiteDB.LiteEngine..ctor(IDiskService disk, String password, Nullable1 timeout, Int32 cacheSize, Logger log)
at LiteDB.LiteDatabase.<>c__DisplayClass11_0.<.ctor>b__0()
at LiteDB.LazyLoad1.get_Value()
at LiteDB.LiteCollection1.<Find>d__17.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToListTSource
at OvalCFuwp.Domain.Concrete.DatabaseService.GetAllProfileNames() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\Domain\Concrete\DatabaseService.cs:line 38
at OvalCFuwp.Domain.Concrete.CredentialRepository.GetProfileNames() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\Domain\Concrete\CredentialRepository.cs:line 30
at OvalCFuwp.MainPage.UpdateProfileList() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\MainPage.xaml.cs:line 82
at OvalCFuwp.MainPage.Init() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\MainPage.xaml.cs:line 61
at OvalCFuwp.MainPage..ctor() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\MainPage.xaml.cs:line 38
at OvalCFuwp.OvalCFuwp_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_MainPage() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\obj\x86\Debug\XamlTypeInfo.g.cs:line 184
at OvalCFuwp.OvalCFuwp_XamlTypeInfo.XamlUserType.ActivateInstance() in C:\Users\Alex.White\Source\Repos\OvalCFuwp\OvalCFuwp\obj\x86\Debug\XamlTypeInfo.g.cs:line 353
````
I have tried a variety of things like changing the directory, using a stream from a StorageFile and even creating a dummy db file, but I can't seem to get it to run without this exception
LiteDB tries to open file in application folder, near dll/exe.
But UWP-apps work in sandbox and can't open any file in any folder.
So you cannot create LiteDatabase in this way.
You need to use full path to open .db file.
Also, your app needs to have access to folder with .db file.
By default, UWP-apps have access to LocalFolder
For example:
```C#
private string Filename { get { return "credentials"; } }
private string DBName { get { return @"OvalCF.db"; } }
public IEnumerable
{
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var folderPath = localFolder.Path;
var filePath = Path.Combine(folderPath, this.DBName);
using( var db = new LiteDatabase( filePath ) )
{
return db.GetCollection
.FindAll().ToList();
}
}
```
Also, you can use KnownFolder API (Documents, Videos, Music...) or FolderPicker with FutureAccessList API.
Very clear explanation, suggest closing the issue.
Please add a comment about this issue on https://github.com/mbdavid/LiteDB/wiki/Getting-Started
Closing this issue as it looks like it is resolved.
Most helpful comment
LiteDB tries to open file in application folder, near dll/exe.
But UWP-apps work in sandbox and can't open any file in any folder.
So you cannot create LiteDatabase in this way.
You need to use full path to open .db file.
Also, your app needs to have access to folder with .db file.
By default, UWP-apps have access to LocalFolder
For example:
```C#
private string Filename { get { return "credentials"; } }
private string DBName { get { return @"OvalCF.db"; } }
public IEnumerable GetAll()( this.Filename )
{
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var folderPath = localFolder.Path;
var filePath = Path.Combine(folderPath, this.DBName);
using( var db = new LiteDatabase( filePath ) )
{
return db.GetCollection
.FindAll().ToList();
}
}
```
Also, you can use KnownFolder API (Documents, Videos, Music...) or FolderPicker with FutureAccessList API.