简体   繁体   中英

Windows Phone 8.1 access file in Documents folder

I'm new to windows phone development, and not using silverlight or WPF. I copy the file "links.txt" into my Windows Phone folder at "\\Documents\\" and I want to access and get the content in the file, but I'm getting back as access denied error. I click on Package.appxmanifest file then select "Capabilities" tab, but I don't see "Documents Library Access" for me to check it. As matter fact I don't see any "... Library Access" showing. Below is the code:

string fileName = "\\Documents\\links.txt";
string parentPath = ApplicationData.Current.LocalFolder.Path;
string filePath = Path.Combine(parentPath, fileName);
StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);

Any suggestion how can I read the file? thanks.

Updates:

It seems the code above does not work, but when using the code below and change the folder to "Music" instead of "Documents" then check the Capabilities for "MusicLibrary", it's working.

 var folder = KnownFolders.MusicLibrary;
 var file = await folder.GetFileAsync("links.txt");
 var read = await FileIO.ReadTextAsync(file);

Thanks!

You are give the wrong filePath actual path is in this image

在此输入图像描述

i try your code it shows same error so correct your file path

If you checked need Capability for Documents you should use KnownFolders class instead ApplicationData.Current.LocalFolder.Path .

For example:

 StorageFolder storageFolder = KnownFolders.DocumentsLibrary; 
 StorageFile file = await storageFolder.CreateFileAsync("sample.dat", CreationCollisionOption.ReplaceExisting); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM