简体   繁体   中英

How to get file in WinRT from project path?

I want to add data files to my project. Those files will not change in the future. Where in project structure I can place them? I tried to put them into Assets , but sometimes they dissapear from there. Where should I put them?

From your description its not clear whats wrong. You have to provide more details. But I think that putting files to Assets is a right way to do it and it should work. i dont see how they can disappear by themselves. Other option that might work for you is using ApplicationData class and methods and properties that are there. You can find it in Windows.Storage namespace. You can find more details about ApplicationData on MSDN http://msdn.microsoft.com/en-us/library/windows/apps/hh700361.aspx .

EDIT: Loading files from Assets can by done for example like this

var uri = new System.Uri("ms-appx:///Assets/YourFile.txt");
var file = Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

You can find more details on MSDN http://msdn.microsoft.com/en-us/library/windows/apps/hh965322.aspx .

Check the file and the folder's properties (through visual studio) and check that "Build Action" is set to Content and "Copy to Output Directory" to "Copy Always"

在此处输入图片说明

To read this file, then use:

var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("MyFolder");
var file = await folder.GetFileAsync("MyFile.txt");

if (file != null)
{
    var content = await FileIO.ReadTextAsync(file);
}

Source: Windows 8: The right way to Read & Write Files in WinRT

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