简体   繁体   中英

Reading from / Writing into a Text file in WP8 App

I'm familiar with the Data File Handling concept involved in C, C++ and also have implemented it in Console Applications using the System.IO namespace and the necessary classes involved.

However, I am not able to do so in a Windows Phone 8 project.

What I'd like is to directly add an already created text file to my Windows Phone 8 project and perform the read and write operations as per my need. (Not to be confused with working with files through the Isolated Storage)

I found some solutions where the file is read as a whole into one variable, however, this does not solve my purpose. I want some sort of control in the file so that I can traverse to the exact line number and read the particular line into a local string variable. I'd also like to detect EOL and EOF.

Is it possible to implement this to the exact level of detail that I require?

It would also be very helpful if you'd explain the difference between a text file present in the Isolated Storage and a text file directly present in the Project.

Kindly help me out. Thanks!

So, I tried somethings on my own and came up with a solution to my problem.

Well the Data File handling mechanism in C# is pretty "intelligent" and the approach used for a WP8 App is similar to that of a Console Application.

We use the System.IO namespace along with the StreamReader Class. The ReadLine() function is used to read a particular line. The reading process starts from the beginning of the file to the end of file. A counter is used for a specific line that needs to be read/fetched.

Hope this is somewhat useful for someone who faced a similar problem.

The code that worked for me was:

var str = Application.GetResourceStream(new Uri("Sample_File.txt", UriKind.Relative));
            StreamReader sreader = new StreamReader(str.Stream);

            int x = 0;
            while(!sreader.EndOfStream)
            {
                x = x + 1;//increment the counter
                if(x==7) //counter value matches the required number
                {
                    //perform necessary tasks
                    break; //if required
                }
                sreader.ReadLine();
            } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM