c# - await FileIO.ReadTextAsync giving error (edited: now using IStorageFile.OpenReadAsync) -



c# - await FileIO.ReadTextAsync giving error (edited: now using IStorageFile.OpenReadAsync) -

i have method in project. supposed do: accepts string name of file in folder, av, in assets folder of project. opens file , performs operations on (not of required operations in code, yet).

public async static task<book> parsefile(string bookn) { string bookname = bookn.replace(" ", ""); storagefile bookfile = await storagefile.getfilefromapplicationuriasync(new uri("ms-appx:///assets/av/" + bookname + ".txt")); string content = await fileio.readtextasync(bookfile); string chapterstr = "chapter"; int numofchapters = utilities.numoftimesstringoccurs(content, chapterstr); book book = new book(bookname, numofchapters); //book.chapters = new list<chapter>(numofchapters); homecoming book; }

the problem line:

string content = await fileio.readtextasync(bookfile);

i know because set breakpoint @ first line of method. after stepping code, particular line broke , opened .g.cs file , highlighting line apparently implied compiler not trace it. line had diagnostics.debugger or that.

the next step tried set in try , display error in messagedialog. error was:

object reference not found

or similar.

i thinking line

storagefile bookfile = await storagefile.getfilefromapplicationuriasync(new uri("ms-appx:///assets/av/" + bookname + ".txt"));

is causing since has await maybe operation not finish before next line executed. don't expect slow since files in folder 5 - 6 kb each on average.

i don't know what's wrong first time of using storage namespace classes i'm new windows store programming.

edit

after going through msdn forum, changed code this:

storagefile bookfile = await storagefile.getfilefromapplicationuriasync(new uri("ms-appx:///assets/av/" + bookname + ".txt")); windows.storage.streams.irandomaccessstream ras = await bookfile.openreadasync(); windows.storage.streams.datareader reader = new windows.storage.streams.datareader(ras.getinputstreamat(0)); int numofbytes = (int)ras.size; byte[] bytes = new byte[numofbytes - 1]; await reader.loadasync((uint)numofbytes); reader.readbytes(bytes); string content = convert.tobase64string(bytes);

this code should have worked because when stepped through during breakpoint, there no error. reason can't fathom, app doesn't react. when set breakpoint , step code, works (i mean doesn't break; not give me expected output) when there none, "freezes".

i thought size of file (201 kb) taking long changed much lesser size (~3 kb) same. i'm thinking has async , await. wish there way without using them.

edit: i'm not posting reply because realized solved issue question did not contain plenty info answer.

the solution didn't utilize await on method calling method. have deleted thought maybe help someone. attempted solve it.

first can check property of file(assets/av/someone.txt).

the build action should content. not embedded resource or others.

if still error. check if file existed.

c# windows-store-apps async-await

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -