yohannist
yohannist

Reputation: 4204

Access denied overwriting a file in winrt c#

I am trying to overwrite the content of a file but it keeps throwing access denied

System.IO.Stream s2 = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForWriteAsync("Assets\\xyz.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
System.IO.StreamWriter sw = new StreamWriter(s2);

How can i make it work?? The exception is

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.IO.WindowsRuntimeStorageExtensions.<OpenStreamForWriteAsyncCore>d__10.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  at System.IO.WindowsRuntimeStorageExtensions.<OpenStreamForWriteAsyncCore>d__10.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at

Upvotes: 1

Views: 1510

Answers (2)

marcusdev
marcusdev

Reputation: 11

You'll need to write to a location like this: ApplicationData.Current.TemporaryFolder

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500635

You can't write to the area where the package is installed, by the looks of it. From "App packages and deployment":

Windows Store app deployment

The Windows Store app model is a declarative state-driven process that provides all installation and update data and instructions for an app in a single package. In this declarative model, deployment operations are reliable. The files shipped in the package are immutable, which means that they haven't been modified since they were delivered to the computer. Because the package owner doesn't need to write custom actions and code, the number of failure points are reduced.

If you need to update the assets for all users, it sounds like you should just publish a new version, e.g. via the isolated storage API.

If you need to write user-specific data, you'll need to pick a different location.

Upvotes: 3

Related Questions