Temporary folder access denied - c#

I started with UWP platform apps and I want create new folder in Temp folder:
StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
temporaryFolder = await temporaryFolder.CreateFolderAsync
(Path.GetFileNameWithoutExtension(Path.Combine(temporaryFolder.Path, fileName)),
CreationCollisionOption.ReplaceExisting);
Everything looks OK, but when I want to decompress a ZIP file, which is in Temporary folder into folder which one I created then I give exception:
System.UnauthorizedAccessException: Access to the path 'C:\Users\Admin\AppData\Local\Packages\cebff192-8162-4800-9f9c-b3ce1ca8849f_5gyrq6psz227t\TempState\1' is denied.
My question is simple: How I can create a new folder in Temp to which I can write?

You should be able to access the file in the temporary Folder, but it depends on how you access it. When you access the file, please avoid using the file path. See this blog: https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/
To open and read a file in the temporary app data store, use the file APIs, such as Windows.Storage.StorageFolder.GetFileAsync. You can get more details about the temporary folder from Temporary app data.

Related

UWP StorageFile.OpenAsync Access Denied Issue

I'm running into an issue where I can read from a file in the app install directory but can't write to it.
In the below code, I open a file for reading, do stuff, dispose of my stream pointers, then try to open the file for writing.
//Open file for reading
var SocStorageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///LastSoc.txt"));
var SocInputStream = await SocStorageFile.OpenReadAsync();
var SocClassicStream = SocInputStream.AsStreamForRead();
var SocStream = new StreamReader(SocClassicStream);
<do stuff with file read>
.....
SocInputStream.Dispose();
SocClassicStream.Dispose();
SocStream.Dispose();
//Open record file for writing
var RandomAccessStream = await
SocStorageFile.OpenAsync(FileAccessMode.ReadWrite);
When I try the last line to enable write access, I get:
System.UnauthorizedAccessException: 'Access is denied. (Exception from
HRESULT: 0x80070005 (E_ACCESSDENIED))'
Worried that my Dispose() methods didn't completely clean it up, I tried commenting out all my read accesses. Same error. (Yeah I know, i should use 'using' but I don't think that's the problem here)
I'm not sure why I can't obtain write access. Advice appreciated thanks!
From the path specified in your code, it looks like you are attempting to write to a file that is part of your app's package. This file exists in the installation folder of your app which you only have read only access to.
Simply put, you can read application files in the installation directory but you can't write to files in that directory. This is also true for creating new files in the app installation directory.
Have a look at this doc from Microsoft explaining file access in UWP: https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions
From the doc linked above, the important part is:
The app's install directory is a read-only location. You can't gain access to the install directory through the file picker.
Now, if you are planning on updating this file's content, you should move it to the Application Data folder ( ApplicationData.Current.LocalFolder ). UWP apps are allowed to read and write (and create new files) in that directory.

How can I access the current application folder in UWP?

I need to access a subfolder in the application folder whose path is:
C:\Users\myname\Documents\Visual Studio 2015\Projects\App2\App2\Templates
and retrieves all the data inside. I need to read the xml files in a subfolder of it, parse them, and store them in a dictionary. I have the parsers ready but just don't know how I can access the folder?
I tried
StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder templateFolder = await localFolder.GetFolderAsync("Templates");
But it didn't work. An error occurred indicating that folder "Templates" could not be found. However, when I tried "Assets" instead, which had been in the folder ever since the project was created, it worked. How can I access it?
If Template folder is in your project, then you can get it via:
var TempFile = "ms-appx:///Templates/Yourfile.jpg";

saving file in Assets folder in Windows 8 App

I am developing an App for Windows Store.
I have to save a recorded media as "file1.mp3" into the Assets folder without opening the save file prompt. I used the following code
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFileAsync(#"Assets\file1.mp3",Windows.Storage.CreationCollisionOption.ReplaceExisting);
However the system returns an "access denied" error. The file has to be saved in the Assets directory only and I do not want to use FileSavePicker. Please Help.
Thanks
Your package folder is read-only. Use your app data folders from Windows.Storage.ApplicationData.Current instead, wherein you'll find a LocalFolder and TemporaryFolder (also RoamingFolder but an mp3 would exceed the 100KB roaming limit). Then you can use either folder's CreateFileAsync.

Windows RT 8 - view files in application storage

So I'm writing an xml file to the local Storagefolder
using Windows.Storage;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
And i would like to view this file in the Windows Explorer for debug purposes.
Is there any way?
It is located under
C:\Users\*Username*\AppData\Local\Packages\*application specific letters and numbers*\LocalState
You can easily determine the path by exploring the Path property of your localFolder object.
All your application data are in \Users\\Appdata\Local\Packages.
Choose your application, and go in the LocalState folder.

How to access home directory and auto create directory on install

I want to create a directory with some config file on the installation process and user home directory. My questions are:
-> how to create this directory while installing
-> how to access home directory from c# code
You have access to some special folders for your application. You can save a file to the LocalFolder like this:
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("config.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, "Text to write to file");
and then retrieve it:
file = await ApplicationData.Current.LocalFolder.GetFileAsync("config.txt");
There's also a RemoteFolder for files you want available to the user if they use your application on another device.
This folder can be found by going to something like "C:\Users\USERNAME\AppData\Local\Packages" and then finding the folder for your app.

Categories

Resources