mszubart
mszubart

Reputation: 117

Windows 8 App and access to file system

I'm at the beginning of my project and I wonder which technology I should use. In my little research I found WinRT API being kind of pleasant and I really like tile grid concept in UI.

The only problem is that my app will generate tons of data - important data - which I have to store somewhere on the local machine. By 'somewhere' I mean use of a different partition than the OS.

So, why not to try this simple code.

await Windows.Storage.PathIO.WriteTextAsync(@"d:\tests\test.txt", "Hello World");

Because E_ACCESSDENIED, that's why. Windows 8 slaps me in face screaming "Access Denied".

Is there any way I can store my data in a way I like or Win8 is too h4x0r proof?

And no, "Make a desktop application" is not a correct answer.

Upvotes: 3

Views: 7271

Answers (2)

Lennart Jansson
Lennart Jansson

Reputation: 61

First of all, when storing config data you have two options:

Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

Which will use the roaming profile space so it will be stored in Cloud or Domain Profile

Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;

Which will use the local profile space

Of course they both will be stored in the end under your users %appdata% but the roaming one will actually be synched if I understand everything correctly :)

So, for the application data that you would like to store on another partition:

First you need to select the location by using a FolderPicker

var folderPicker = new Windows.Storage.Pickers.FolderPicker();
//Add some other yada yada to make the picker work as needed
StorageFolder folder = await folderPicker.PickSingleFolderAsync();

Then you need to put the selected folder in an access list to remember that it's allowed to use this folder

StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);

That way the application / system will keep track that it's allowed to use this folder in the future. The selected folder could be anywhere in the file system where you have access.

Finally if you wan't to get the selected folder back next time the app starts you simply do the opposite:

StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("TargetFolderToken",AccessCacheOptions.FastLocationsOnly);

The value FastLocationsOnly means that it will only return local drives. "TargetFolderToken" is the same identifier you used when you stored the folder in the FutureAccessList.

Upvotes: 3

spender
spender

Reputation: 120518

All you need to know about file access and permissions in Windows Store Apps.

Upvotes: 3

Related Questions