I'm trying to create the database file to initialize it but I'm getting "Access Denied" error during execution. I think is something related with permissions but I'm not able to find any information that could fix it, since every guide I check does it the same way.
This is an UWP application using C# and SQLite library. Thanks in advance for the help.
In the past I've tried to also create json file with other methods but I've never been able to create a file from a UWP application
Code that raises an error:
private const string DBName = "data.sqlite";
if (!File.Exists(Path.GetFullPath(DBName)))
{
SQLiteConnection.CreateFile(DBName);
RecentlyCreated = true;
}
Error message:
System.UnauthorizedAccessException: 'Access to the path 'C:\Users\myuser\source\repos\Pass\Pass\bin\x64\Debug\AppX\dataAccount.sqlite'
is denied.'
UWP has a very limited access to the user filesystem, as it described at File access permissions specification. You can use user libraries (downloads, documents) for storing the data (after allowing an access in appxmanifest file) or application data store (more viable in your case). Try to define DBName as
string DBName = ApplicationData.Current.LocalFolder.Path + #"\data.sqlite";
Or you can allow broadFileSystemAccess using link above
Related
I am migrating a library from .NET Framework to .NET Core. In this library, I offer a convenient way to save XML data in the user's AppData folder.
When running under .Net Core, I get an exception:
Access to the path 'C:\Users' is denied
The value of _userDataPath at that time is:
C:\Users\cedbo\AppData\Roaming\MyApp
This works fine under .NET Framework, so it is not an authorization problem.
// compute filepath
_userDataPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
appName);
_userDataFile = Path.Combine(_userDataPath, "UserData.xml");
// load data or create file if required.
T userData = null;
if (File.Exists(_userDataFile)) // !!! Returns false even when file exists
{
userData = XmlTools.GetFromXml<T>(_userDataFile);
}
else
{
userData = new T();
if (!Directory.Exists(_userDataPath)) // !!! Returns false even when directory exists
{
Directory.CreateDirectory(_userDataPath); // Exception: Access to the path 'C:\Users' is denied.
}
XmlTools.SaveToXml<T>(userData, _userDataFile);
}
I am ready to use another standard approach if .NET Core proposes one, but I didn't find anything neither on this write access limitation, nor on another way to store user data recommended in .NET Core.
Can anyone please advise?
Thank you in advance.
Here are my findings:
I was trying to run this code from a UWP application. I tried but couldn't write anything either on other locations such as C:\ or D:\ drives (D: is a second hard drive).
I eventually found that UWP files access if different from what I used to do with WPF for instance: https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions
For instance, ApplicationData.Current.LocalFolder will return a StorageFolder object that contains methods such as CreateFile, GetItemsAsync, etc. This is typically the place where I would store user data if I wanted to keep it in a file. See https://learn.microsoft.com/fr-fr/uwp/api/windows.storage.storagefolder?view=winrt-19041 for more information.
Given this substantial change in the way I'd store user data, I will consider using EntityFrameworkCore on a sqlite database instead:
https://learn.microsoft.com/en-us/windows/uwp/data-access/sqlite-databases
I hope that sharing this adventure will help others facing the same situation.
Happy coding to all!
I am trying to convert an Excel document into PDF in a ASP.Net Web application using Pdftron (Pdfnet) library.
Following is my code for the above purpose.
pdf.PDF.PDFDoc newSalaryFitmentPdf = new pdf.PDF.PDFDoc();
pdftron.PDF.Convert.ToPdf(newSalaryFitmentPdf, newSalaryFitmentExcel.FullName);
salaryFitment = newSalaryFitmentPdf.Save(pdf.SDF.SDFDoc.SaveOptions.e_linearized);
But I am getting the following exception when the program is trying to execute the second line.
Exception:
Message: An error occurred while converting the file.
Detailed error:
Error creating a new Excel application instance.
Code: PDFTRON_UNKNOWN (-2147024891)
File: "Excel.cpp":51
Log:
Start check system account.
Session ID is: 0.
Failed to create Desktop folder in SystemProfile. boost::filesystem::create_directory: Access is denied: "C:\Windows\system32\config\systemprofile\Desktop"
Failed to create Desktop folder in SystemProfile. boost::filesystem::create_directory: Access is denied: "C:\Windows\SysWOW64\config\systemprofile\Desktop"
Done checking system account.
Creating an Excel application instance.
Conditional expression: false
Version : 6.7.1.61823N
Filename : Convert.cpp
Function : trn::PDF::Convert::ToPdf
Linenumber : 1692
Any help would be highly appreciated.
Failed to create Desktop folder in SystemProfile. boost::filesystem::create_directory: Access is denied: "C:\Windows\system32\config\systemprofile\Desktop"
Failed to create Desktop folder in SystemProfile. boost::filesystem::create_directory: Access is denied: "C:\Windows\SysWOW64\config\systemprofile\Desktop"
You must set your application to have read/write access to these directories. Please see this article for further information.
I am exporting an excel using c# code and spreadsheetlight dll.
I am trying to insert a picture into excel, everything works well on local machine but on live web site it gives following error
Access to the path 'F:\sitesroot\0\Images\Logo.png' is denied
My c# code.
string filePath = Server.MapPath("~/Images/Logo.png");
SLPicture pic = new SLPicture(filePath); //SLPicture - An inbuilt class for inserting image
Currently what I am doing is manually providing access rights on IIS server but which is not the correct way to go since each time I upload the service I have to redo that again and again
Any expert advice - How I can provide access right through code?
Or How to get rid of this error.
Thanks.
Assuming you are using Cloud services...
You are supposed to use Local Storage for this (http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx).
Basically;
In your Cloud project, go to the properties for the Role and select Local Storage.
Add a new local storage and give it a name, i.e. "foo" and a maximum size. If this is for temporary processing rather than caching, you probably want to tick "Clean on role recycle".
In your code call RoleEnvironment.GetLocalResource("foo") to get the folder name you can use and then do something like Path.Combine("path","Logo.png") to get a file name which you can then write to.
I've been writing a Windows 8 Store App using XAML and C# and the target device is a Windows Surface tablet.
Within my application I use a SQLite database.
To create/load the database I use the following code:
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "MyDatabase.sqlite");
However I'd like to store the database in the Documents folder so I tried the follow line of code to replace my line above :
DBPath = Path.Combine(Windows.Storage.KnownFolders.DocumentsLibrary.Path, "MyDatabase.sqlite");
When I try this I get the following error:
WinRT information: Access to the specified location (DocumentsLibrary) requires a capability to be declared in the manifest.
So I open Package.appxmanifest and select Documents Library under capabilities and under the declarations tab I've added a File Type Association. Then under Properties I've entered
sqlite in the Name box and .sqlite in the File type box. I've entered no other information on the Package.appxmanifest screen.
Now when I run the app I receive the following error:
Could not open database file: MyDatabase.sqlite (CannotOpen)
When I inspect Windows.Storage.KnownFolders.DocumentsLibrary.Path it equals "". I would have expected the full location path here.
Can anyone help and has anybody ever saved a SQLite database to the documents folder?
Thanks in advance.
KnownFolders.DocumentsLibrary does not have a path since it's a virtual location. Take a look at this answer for more details. This could make it impossible to open a SQLite database there, at least using the APIs available in sqlite-net library.
Also before using access to Documents library in your Windows Store app keep in mind that for publishing an app with this capability to the store you need to have a company account. With an individual account such applications will be rejected.
I am trying to using the ESENT windows database with the managedesent library but I always get the error
Error FileAccessDenied (JET_errFileAccessDenied, Cannot access file, the file is locked or in use)
The code to open the database is
m_Dictionary = new PersistentDictionary<string, PropertyStruct>("BaseEstateCachedPropertySummary2");
I am testing this on Windows 7 and the application is an ASP.NET application. Does anyone have any ideas about this, documentation is pretty slim.
Are you trying to open an existing database used by another service (e.g. the Windows Update database)? Esent databases are single-process only so you have to stop any other processes that are using the same database.
I found an answer. Apparently I didn't have write permissions to the default directory where it puts the database so I had to specify the full path such as.
m_Dictionary = new PersistentDictionary<string, PropertyStruct>(#"c:\Data\BaseEstateCachedPropertySummary2");