hanssens / localstorage

LocalStorage for .NET - A simple and lightweight tool for persisting data in dotnet (core) apps.
MIT License
74 stars 17 forks source link

Permission error #30

Closed thomas-tewolde closed 3 years ago

thomas-tewolde commented 4 years ago

Hi I cant perform any method unless I have an admin privilege, is it possible to save the storage file in a user defined path.

Message: An unhandled exception occurred.
Exception:
System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\xxxxx\.localstorage' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at Hanssens.Net.LocalStorage.Persist()
   at xxxx.xxxx.xxxxx.<Login>d__6.MoveNext() in D:\xxxx\xxxx.cs:line 53
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
hanssens commented 4 years ago

Thanks for the report @thomas-tewolde. To reproduce, can you provide the Exception details, please?

thomas-tewolde commented 4 years ago

@hanssens thanks, i Have updated the issue description with the Exception thrown.

thomas-tewolde commented 4 years ago

I suggest a solution for this to pass file as a stream instead

using (var stream = System.IO.File.OpenRead(@"C:\Users\xxx\Desktop\.env"))
{
     LocalStorage.Load(stream);
}
hanssens commented 3 years ago

In the stacktrace I see you where attempting to write to 'C:\Program Files (x86)\xxxxx.localstorage'. That would require elevated permissions indeed. It's better to perhaps use %APPDATA% or some similar location that's meant for this.

Writing to a system directory, like %PROGRAMFILES%, will not be possible without having elevated permissions.

Next, if your suggestion to improve loading the file as a stream, by all means please setup a PR for it. I'll happily look at it accordingly.