I’m not surprised there are only a few thousand Windows Store Apps at the moment. I’m trying to simply copy, access, or just make available, some files stored in a WinRT component project into the main Metro/XAML/Windows 8 Store App. It can’t be that difficult, but google (or bing!) hasn’t been much help so far. Marking the files as ‘Content’ gets half the job done, and the coding part requires:
var appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var tempFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
Some slightly convoluted code: it turns out that there are a couple of ways of accessing the files (note: NOT reading them):
namespace My.Convoluted.Namespace
{
public sealed class Example
{
public Example()
{
var file = LoadFileAsync();
file.Wait();
var fileContents = file.Result;
}
async private Task<StorageFile> LoadFileAsync()
{
StorageFile file1 = null;
StorageFile file2 = null;
try
{
// first way:
var appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var src = @"My.Convoluted.Namespace\Assets\some\nested\path\testfile.txt";
file1 = await appFolder.GetFileAsync(src);
// second way:
file2 = await StorageFile.GetFileFromApplicationUriAsync(new System.Uri("ms-appx:///My.Convoluted.Namespace/Assets/some/nested/path/testfile.txt"));
}
catch (System.IO.FileNotFoundException exception)
{
System.Diagnostics.Debug.WriteLine(String.Format("File doesn't exist. Use default. {0}", exception.Message));
}
return file1;
}
}
}
You need to ensure that the
testfile.txt is part of your solution, and via its properties, it is marked as ‘Content’. Finally, to read a file just do this:
string uri = “ms-appx:///My.Convoluted.Namespace/Assets/some/nested/path/testfile.txt”;
string contents = await PathIO.ReadTextAsync(uri);
Interestingly,
ReadTextAsync takes a string which is a uri, whereas GetFileFromApplicationUriAsync takes a System.Uri, which is, well, a uri.