简体   繁体   中英

Unable to access files on Windows 8.1 phone device

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?

尝试如下使用流阅读器:

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

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.

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