Laurent Resman Laurent Resman - 9 months ago 74
C# Question

Creating a file/folder in UWP

first of all I really hope I'm not asking already answered question, I did my research on stack overflow and beyond and so far didn't find answer to my question.

I finally accomplished that my Universal windows (10) app makes a project folder for user, inside LocalState folder, but its fine as long as it stays there. Now, I'm trying to generate some simple text file and save in "Project" folder, example path: LocalState\Project1\example.txt.

I tried with following code:

StorageFolder mapa_projekta = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await mapa_projekta.CreateFileAsync("sample.txt");

But it only saves to LocalState directory, maybe there is simple solution of adding path to the sub folder, but so far I didn't succeed in that.

Sorry for my poor english and thanks for all your time

Wish you all nice day

Answer Source

First, we can make the project folder:

StorageFolder rootFolder = ApplicationData.Current.LocalFolder;
var projectFolderName = "Project1";
StorageFolder projectFolder = await rootFolder.CreateFolderAsync(projectFolderName, CreationCollisionOption.ReplaceIfExisting);

Then, you can create your file within your subfolder:

StorageFile sampleFile = await projectFolder.CreateFileAsync("sample.txt");