Answered by:
Wanting to read a file from my Metro Project / How?

Question
-
I have a Metro project that I've put a file in the root directory + "data" called:
C:\VSProjects\USGov\data\myfile.json
I want to read this file and can't figure out how to. I can read files in a temp directory using code like I have below, but I want to include this file in my distribution.
Once I figure this out, I'm going to be hit with a second problem which is readString fails with an error on my JSON. How can I read this large json file (.5MB) without getting errors.
dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) { var size = stream.size; if (size == 0) { // Data not found } else { var inputStream = stream.getInputStreamAt(0); var reader = new Windows.Storage.Streams.DataReader(inputStream); reader.loadAsync(size).then(function () { var contents = reader.readString(size); ...
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsiderMonday, January 2, 2012 9:36 PM
Answers
-
Hi Jeff,
"StorageFile sf = await Package.Current.InstalledLocation.GetFileAsync" means nothing to me. Is Package a namespace? it does not show up in the WinRT api list for JavaScript (or c# for that matter which is where it is posted).
Can you please give me an example in JavaScript similar to my code above that will allow me to open and read the file. I combined it in one questions because open and read are usually together in the same example. If you still want me to create another thread for this question I will but to me it seems unnecesary.
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider- Marked as answer by Peter Kellner Tuesday, January 3, 2012 7:23 PM
Tuesday, January 3, 2012 4:39 PM -
Sorry Peter I go back and forth between the two I forgot where you were posting and put in the C# thread.
The documentation has an example of how to use these objects for each language (normally).
Here is the documentation for that API:
You should be able to use that the same way you are using storage folders in javascript today.
If you need some examples for using storage folders, check out the documentation or samples and they should help you along (any of the picker examples should be adequate!
-Jeff
Jeff Sanders (MSFT)- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, January 3, 2012 4:49 PM
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, January 3, 2012 7:24 PM
Tuesday, January 3, 2012 4:49 PMModerator
All replies
-
Hi Peter,
You can get the install location and read the files from there:
Please create another thread for your other question.
-Jeff
Jeff Sanders (MSFT)- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, January 3, 2012 4:04 PM
Tuesday, January 3, 2012 4:04 PMModerator -
Hi Jeff,
"StorageFile sf = await Package.Current.InstalledLocation.GetFileAsync" means nothing to me. Is Package a namespace? it does not show up in the WinRT api list for JavaScript (or c# for that matter which is where it is posted).
Can you please give me an example in JavaScript similar to my code above that will allow me to open and read the file. I combined it in one questions because open and read are usually together in the same example. If you still want me to create another thread for this question I will but to me it seems unnecesary.
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider- Marked as answer by Peter Kellner Tuesday, January 3, 2012 7:23 PM
Tuesday, January 3, 2012 4:39 PM -
Sorry Peter I go back and forth between the two I forgot where you were posting and put in the C# thread.
The documentation has an example of how to use these objects for each language (normally).
Here is the documentation for that API:
You should be able to use that the same way you are using storage folders in javascript today.
If you need some examples for using storage folders, check out the documentation or samples and they should help you along (any of the picker examples should be adequate!
-Jeff
Jeff Sanders (MSFT)- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, January 3, 2012 4:49 PM
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, January 3, 2012 7:24 PM
Tuesday, January 3, 2012 4:49 PMModerator -
I've recreated the post with the multi-byte error here:
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsiderTuesday, January 3, 2012 7:29 PM -
Thanks Jeff! I just did a post explaining everything you explained with working source. Hopefully, i if Microsoft does not change all this when beta and production come a long, it might help someone else.
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsiderTuesday, January 3, 2012 9:28 PM -
That is awesome Peter, thanks for helping the Community with your Post!
-Jeff
Jeff Sanders (MSFT)Tuesday, January 3, 2012 9:30 PMModerator -
You are most welcome. More to come! I'm trying to build a pretty straight forward app for the store contest which I need to finish this week (deadline 1/8). I'm planning on making it public and doing lots of posts about it (win or not). I need to get back to my SVG stuff now (not Canvas) so I can make the state outline.
Here is what it currently looks like on the first page:
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsiderTuesday, January 3, 2012 9:38 PM -
I cannot seem to get this to work with the full release??
I keep getting access denied?
Monday, November 12, 2012 5:09 PM