简体   繁体   English

在WP8 App中读取/写入文本文件

[英]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. 我熟悉C,C ++中涉及的数据文件处理概念,并且已经使用System.IO命名空间和所涉及的必要类在控制台应用程序中实现了它。

However, I am not able to do so in a Windows Phone 8 project. 但是,我无法在Windows Phone 8项目中执行此操作。

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. 我想要的是直接将一个已经创建的文本文件添加到Windows Phone 8项目中,并根据需要执行读取和写入操作。 (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. 我还想检测EOL和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. C#中的数据文件处理机制非常“智能”,并且WP8应用程序使用的方法类似于控制台应用程序。

We use the System.IO namespace along with the StreamReader Class. 我们将System.IO命名空间与StreamReader类一起使用。 The ReadLine() function is used to read a particular line. ReadLine()函数用于读取特定行。 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();
            } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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