如何从资产文件夹中检索数据

时间:2019-04-07 22:01:40

标签: c# file exception file-io uwp

我正在构建一个应用程序,该应用程序以 JSON 格式在Assets文件夹中存储了一些数据。现在,当我的应用程序满足特定要求时,它应该读取文件,反序列化并显示数据。

这是我编写的代码-

StorageFile file = await StorageFile.GetFileFromPathAsync(@"Assets\appleTree.json");
string data = await FileIO.ReadTextAsync(file);
var serializer = new DataContractJsonSerializer(typeof(DataModel));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
var result = (DataModel)serializer.ReadObject(ms);

问题是,在第1行,程序抛出此异常-

值不在预期范围内

如何解决此问题?

  

在此发布之前,我在线搜索,找不到满意的答案。

1 个答案:

答案 0 :(得分:1)

您需要使用// We're going to read 83 edges: // - First a FALLING, RISING, and FALLING edge for the start bit // - Then 40 bits: RISING and then a FALLING edge per bit // To keep our code simple, we accept any HIGH or LOW reading if it's max 85 usecs long uint16_t rawHumidity = 0; uint16_t rawTemperature = 0; uint16_t data = 0; for ( int8_t i = -3 ; i < 2 * 40; i++ ) { byte age; startTime = micros(); do { age = (unsigned long)(micros() - startTime); if ( age > 90 ) { error = ERROR_TIMEOUT; return; } } while ( digitalRead(pin) == (i & 1) ? HIGH : LOW ); if ( i >= 0 && (i & 1) ) { // Now we are being fed our 40 bits data <<= 1; // A zero max 30 usecs, a one at least 68 usecs. if ( age > 30 ) { data |= 1; // we got a one } } switch ( i ) { case 31: rawHumidity = data; break; case 63: rawTemperature = data; data = 0; break; } } // Verify checksum if ( (byte)(((byte)rawHumidity) + (rawHumidity >> 8) + ((byte)rawTemperature) + (rawTemperature >> 8)) != data ) { error = ERROR_CHECKSUM; return; } here)方法访问StorageFile.GetFileFromApplicationUriAsync文件夹。例如:

Assets

(请参阅 Mark Heath's blog post with this topic