Create folders and files in Metro

Source: Internet
Author: User

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 !!!

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.