[UWP]将json文件反序列化为UWP应用程序。 mscorlib.ni.dll中出现“System.IO.FileNotFoundException”类型的异常,但未在用户代码中处理 [英] [UWP]Deserialize json file into UWP app. An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code
问题描述
嗨。
我正在尝试将json文件读入我的项目并将其添加到名为CountryCodes的列表中。
I am attempting to read a json file into my project and add it to a list called CountryCodes.
类ReadInFile
  {
      public async static Task deserializeJsonAsync()
      {
          string content = String.Empty;
         列表与LT; CountryCodes> myCodes;
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var jsonSerializer = new DataContractJsonSerializer(typeof(List< CountryCodes>));
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var myStream = await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(" Country-codes.txt");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; myCodes =(List< CountryCodes>)jsonSerializer.ReadObject(myStream);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; foreach(my代码中的var代码)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; content + = String.Format(" Name:{0},Code:{1}",code.name,code.code);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }
&NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; }
class ReadInFile
{
public async static Task deserializeJsonAsync()
{
string content = String.Empty;
List<CountryCodes> myCodes;
var jsonSerializer = new DataContractJsonSerializer(typeof(List<CountryCodes>));
var myStream = await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("Country-codes.txt");
myCodes = (List<CountryCodes>)jsonSerializer.ReadObject(myStream);
foreach(var code in myCodes)
{
content += String.Format("Name: {0}, Code: {1}", code.name, code.code);
}
}
}
公共类CountryCodes
&NBSP; {
&NBSP; &NBSP; &NBSP;公共字符串代码{get;组; }¥b $ b &NBSP; &NBSP; &NBSP;公共字符串名称{get;组; }¥b $ b &NBSP; }
public class CountryCodes
{
public string code { get; set; }
public string name { get; set; }
}
//在UI中按下时启动过程
//When pressed in the UI starts the process
private async void WeatherCityButton_Click(object sender,RoutedEventArgs e)
{
&NBSP; &NBSP; await ReadInFile.deserializeJsonAsync();
private async void WeatherCityButton_Click(object sender, RoutedEventArgs e)
{
await ReadInFile.deserializeJsonAsync();
}
mscorlib.ni.dll中出现'System.IO.FileNotFoundException'类型的异常但未在用户代码中处理。
运行我的代码时出现此错误。
An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code. I get this error when i run my code.
其他信息:系统找不到指定的文件。
Additional information: The system cannot find the file specified.
任何帮助都会很棒。
推荐答案
您收到的错误消息应该是不言自明的。您得到此异常,因为没有名为"Country-codes.txt"的文件。在本地应用程序数据存储中的根文件夹中。此文件夹通常位于C:\ Users \yourname \ AppData \ Local @\\Packages \ 510c9cfa-b93a-4613-9f7c-b148ad4b3898_d9dzbydf825f0 \ LocalState的路径中。
The error message you are getting should be pretty self-explanatory. You get this exception because there is no file named "Country-codes.txt" in the root folder in the local app data store. This folder is typically located at a path like C:\Users\yourname\AppData\Local\Packages\510c9cfa-b93a-4613-9f7c-b148ad4b3898_d9dzbydf825f0\LocalState.
我想你没有在那里保存文件然后就找不到运行时了。有关LocalFolder的更多信息,请参阅以下页面:
https://msdn.microsoft.com / library / windows / apps / br241621
I guess you didn't save your file there and then it won't be found a runtime. Please refer to the following page for more information about the LocalFolder: https://msdn.microsoft.com/library/windows/apps/br241621
希望有所帮助。
请记住通过将有用的帖子标记为答案来关闭您的主题如果您有新问题,请启动新主题。请不要在同一个帖子中提出几个问题
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread
这篇关于[UWP]将json文件反序列化为UWP应用程序。 mscorlib.ni.dll中出现“System.IO.FileNotFoundException”类型的异常,但未在用户代码中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!