This document describes how to create two types of files:
Applicationdata. Current. roamingfolder and knownfolders. documentslibrary.
Before use, you need to reference the namespace using Windows. storage;
1. applicationdata. Current. roamingfolder (applicationdata. Current. localfolder works the same way as this one, but it is saved differently and used differently)
This class only saves files and folders to the directory of the currently logged-on user:
C: \ Users \ admin \ appdata \ Local \ packages \Ebd9cb04-acea-4018-a622-89e102879f03_kwmcxzszfer2y\ Roamingstate \
The name of the folder in red. Different applications do not need to be concerned.
Storagefolder folder = applicationdata. Current. roamingfolder; // obtain the storage path of the current application. c: \ Users \ admin \ appdata \ Local \ packages \Ebd9cb04-acea-4018-a622-89e102879f03_kwmcxzszfer2y\ Roamingstate \
Storagefolder tempfolder = await folder. createfolderasync ("XML", creationcollisionoption. openifexists); // create an XML folder under the above path
Storagefile file = await tempfolder. createfileasync ("test. xml", creationcollisionoption. openifexists); // create a test. xml file in the XML folder.
Note: applicationdata. Current. localfolder works the same way, but is stored in the C: \ Users \ admin \ appdata \ Local \ packages \ ebd9cb04-acea-4018-a622-89e102879f03_kwmcxzszfer2y \ localstate \ folder.
2. knownfolders. documentslibrary
This method is more troublesome and requires something to be set.
Our program requires the permission of the document library, so we need to change package. appxmanifest ,:
Then, set in the declared tab. The file type you want to read and write:
Click Add in the available Declaration on the left, and enter the name and file type in the input column on the right.
Next, we will write the code for creating the file in the background:
Storagefolder folder = knownfolders. documentslibrary; // obtain the path to save the file. The default path is my document root directory c: \ Users \ admin \ Documents ENTs.
Storagefolder tempfolder = await folder. createfolderasync ("XML", creationcollisionoption. openifexists); // create an XML folder C: \ Users \ admin \ Documents \ XML in the preceding path
Storagefile file = await tempfolder. createfileasync ("test. xml", creationcollisionoption. openifexists); // create a test. xml file in the XML folder.
This completes the creation of files and folders.
In addition, you can create a package in the project, in the installation path of the application. current. installedlocation; I tried it. I can only read it and cannot create files or folders. It prompts that the access permission is insufficient ., At present, I am not very good at it. Which of the following experts can give me some advice.
Thank you !!!