如何使用相同的StreamReader两种方法 [英] how to use same StreamReader for two methods

查看:87
本文介绍了如何使用相同的StreamReader两种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,我正在使用流阅读器来读取每一行,但是当我在某些情况下阅读时,我需要调用另一个方法,这个方法将读取下一行来检查其值,然后第一个函数将继续。它正常工作,但在第二种方法我打开新的流阅读器(我正在读同一个文件)所以我怎么能避免打开新的流阅读器并使用打开的一个。当我在第二种方法中读取下一行然后继续通常在第一个mehtod读取(它将读取下一行,在这种情况下,它是用第二种方法检查的行)。

谢谢...



Dears, i am using stream reader to read each line,but when i am reading in some cases i need to call another method and this method will read the next line to check its value,then first function will continue. it is working normally but in the second method i opening new stream reader (i am reading from same file) so how can i avoid to open new stream reader and use the opened one.and when i read the next line in second method then continue reading in first mehtod normally(it will read the next line and in this case it is the line that checked in second method).
thanks...

if (line.StartsWith(Utilities.SCRIPT_COMMAND_CMD, StringComparison.InvariantCultureIgnoreCase)) {
 command = GetCommand(line, Utilities.SCRIPT_COMMAND_CMD, clearScriptPath);
}



 private APDUCommand GetCommand(string scriptLine, string commandType, string clearScriptPath) {
            APDUCommand command = new APDUCommand() { CommandType = commandType };
            command.CommandBody = ReplaceByRegularExpression(scriptLine, commandType, string.Empty);
            string line;
            bool containsScriptLine = false;
            StreamReader reader = null;
            try {
                reader = new StreamReader(clearScriptPath);
                while ((line = reader.ReadLine()) != null) {
                    if (line.Contains(scriptLine) || containsScriptLine) {
                        if (containsScriptLine == true) {
                            if (line.StartsWith("(")) {
                                command.CommandBody += @"\";
                            }
                        }
                        containsScriptLine = true;
                    }
                }
            }
            finally {
                if (reader != null) {
                    reader.Close();
                }
            }
            return command;
        }

推荐答案

如果您使用FileStream,您可以使用共享阅读,请参阅:http://msdn.microsoft.com/en-us/library/5h0z48dh.aspx [ ^ ]

但你最好不要重新打开文件,只需存储职位 [< a href =http://msdn.microsoft.com/en-us/library/system.io.stream.position(v=vs.100).aspx\"target =_ blanktitle =New Window> ^ ]和寻求 []。
If you use FileStream, you can use shared reading, see: http://msdn.microsoft.com/en-us/library/5h0z48dh.aspx[^]
But you better not reopen the file, just store the position[^] and seek[^] to that when coming back from the second method.


这篇关于如何使用相同的StreamReader两种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆