0

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!

Anmol Kumar
  • 90
  • 1
  • 12
  • Did you read the [doc on msdn](https://msdn.microsoft.com/en-us/library/3ak841sy%28v=vs.110%29.aspx) ? – aloisdg Jan 25 '15 at 16:11
  • I did read one with the information related to the Storage Folder class. Is this the required [documentation link](https://msdn.microsoft.com/en-us/library/windows/apps/jj681698%28v=vs.105%29.aspx)? – Anmol Kumar Jan 25 '15 at 16:29
  • Yes It is. Okay I just read again you question. You ask to read on line inside a file, true ? Maybe [this answer](http://stackoverflow.com/questions/1262965/how-do-i-read-a-specified-line-in-a-text-file) can help you. – aloisdg Jan 25 '15 at 16:39
  • The question/answer enclosed is tagged under C#. Are you sure it's applicable to the Windows Phone 8 design and structure? – Anmol Kumar Jan 25 '15 at 16:47
  • Already did....does not work.... – Anmol Kumar Jan 25 '15 at 17:12

1 Answers1

0

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();
            } 
Anmol Kumar
  • 90
  • 1
  • 12