Unable to access files on Windows 8.1 phone device

时间:2016-07-11 19:07:58

标签: c# asp.net json windows-phone-8.1 windows-embedded-8.1

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?

2 个答案:

答案 0 :(得分:2)

尝试在其上使用流阅读器,如下所示:

var myStream = await (await Package.Current.InstalledLocation.GetFolderAsync("Data")).OpenStreamForReadAsync("Users.json");

答案 1 :(得分:0)

检查json文件的属性。右键单击文件 - >属性。

检查复制到输出目录,然后在菜单中选择“始终复制”选项。  这应该会有所帮助。