1

I have problem with reading JSON file. File is inserted into Solution Explorer with Build Action set to Content and copy property se to Copy Always. I would expect then the file should be reachable from app in LocalFolder.

Stream localFolder = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("sources.json");

string json;

using (StreamReader streamReader = new StreamReader(localFolder))
{
    json = streamReader.ReadToEnd();
}

return JsonConvert.DeserializeObject<List<SourceDefinition>>(json);

The error it is returning is:

Error: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

4
  • What's the error you are getting? Commented Feb 27, 2016 at 21:24
  • Error: The system cannot find the file specified. (Exception from HRESULT: 0x80070002) Commented Feb 27, 2016 at 21:26
  • Are you getting the error when running through the Visual Studio or after the app is deployed? Also where exactly in you app did you place the file? Commented Feb 27, 2016 at 21:32
  • During debugging in Visual Studio. The code is placed in Background task (separated project but placed in same solution as main part of my app). I tried to place my sources.json file in both project's root but without any change. Commented Feb 27, 2016 at 21:36

3 Answers 3

1

just try File.OpenRead - you're propably trying to open from AppData which is a special path within thr userprofile (%localappdata% in this case)

Sign up to request clarification or add additional context in comments.

Comments

1

So the ApplicationData.Current.LocalFolder point to the users\XXXXX\Local\Packages\YYYYYY\LocalState where XXXX is the user and YYYY is the GUID for your app. No folder in the project explorer actually points to that location to the best of my knowledge. You can try using the Windows.ApplicationModel.Package.Current. The InstalledLocation.Path will get you access to the bin where the app is running. Hope this helps.

Comments

0

Try this code for reading the json file on a scalable manner and binding to list model class

appsettings.json File

    "Mapping": {
        "Column1": {
          "ExcelColumnName": "DISTRIBUTOR UNIQUE ID",
          "DbFieldName": "vehicle_dealer_code",
          "IsRequired": "true",
          "DataType": ""
        },
        "Column1": {
          "ExcelColumnName": "FACTORY ORDER NUMBER",
          "DbFieldName": "vehicle_data02",
          "IsRequired": "true",
          "DataType": ""
        }
    public static T InitOptions<T>(string name, string section) where T : new()
            {
                var config = new ConfigurationBuilder()
                 .SetBasePath(AppContext.BaseDirectory)
                 .AddJsonFile(name, true, true)
                 .Build();
                return config.GetSection(section).Get<T>();
            }

   

    public class Config
        {
            public Mapping Column1 { get; set; }
            public Mapping Column2 { get; set; }        
        }

public class Mapping
{
    public string ExcelColumnName { get; set; }
    public string DbFieldName { get; set; }
    public bool IsRequired { get; set; }
    public string DataType { get; set; }
}

var MappedData = InitOptions<Config>("appsettings.json", "Mapping");

1 Comment

Hey, this question was from a long time ago and already answered. Your answer is very thorough though and appreciated. Please consider helping with some of the more recent posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.