Reputation: 584
I am using Json files for local storage for my app. While using Visual Studio's emulator reading and writing to the files works correctly. When I connect a device and try to run on that device I cannot access my Json files.
My json files are set to Content and Copy always.
Here is my try statement for reading the file. I have tried two main ways to access the file Current.InstalledLocation
and Uri("ms-appx:///)
. Both seem to work in the emulator but neither work on the device.
try
{
var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder folder = await package.GetFolderAsync("Data");
StorageFile file = await folder.GetFileAsync("Users.json");
//Uri dataUri = new Uri("ms-appx:///Data/Users.json");
//StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
string jsonText = await FileIO.ReadTextAsync(file);
JsonArray jsonArray = JsonArray.Parse(jsonText);
foreach (JsonValue userValue in jsonArray)
{
//Build my model object out of json
}
}
catch(Exception e)
{
//Creating instance for the MessageDialog Class
//and passing the message in it's Constructor
MessageDialog msgbox = new MessageDialog(e.Message);
//Calling the Show method of MessageDialog class
//which will show the MessageBox
await msgbox.ShowAsync();
}
The Output Window Displays:
Exception thrown: 'System.ArgumentException' in mscorlib.ni.dll
Exception thrown: 'System.ArgumentException' in mscorlib.ni.dll
Edit: The try catch loop is not catching the exceptions related to the file system access problem.
On startup when stepping through I am failing out at StorageFile file = await folder.GetFileAsync("Users.json");
Why I trigger the function through a button I fail at string jsonText = await FileIO.ReadTextAsync(file);
The device I am looking to run my application on is running Windows Embedded 8.1 Handheld update 2 (its a ToughPad Panazonic FZ-E1). Do I need to to targeting windows 8.1 instead of windows phone 8.1? It has been working fine with phone up until this point controlling the POS bar-code scanner.
Any help would be appreciated I am at a loss. Could my issue be caused by settings on the device?
Upvotes: 0
Views: 83
Reputation: 263
Try using a stream reader on it like below:
var myStream = await (await Package.Current.InstalledLocation.GetFolderAsync("Data")).OpenStreamForReadAsync("Users.json");
Upvotes: 2
Reputation: 981
Check the properties of the json file. Right click the file-> Properties.
Check the copy to output directory and select copy always option in the menu. That should help.
Upvotes: 0