Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

C#
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;
        }
Posted
Comments
Zoltán Zörgő 3-Apr-13 2:48am    
Why do you open a new reader? Pass the reader to the second method.
youssef.obeid 3-Apr-13 3:09am    
ok but when i back to the first method i have to continue reading and read the line that check in the second method

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900