33
Answers

Reading characters from stream of file

Abhinav Mehta

Abhinav Mehta

13y
8.6k
1
I have a text file created to a certain path.

The contents of the text file are as follow:

%
G70
G91
X.5Y-.5
G97
G41
M04

I want to edit this text file and remove certain characters for eg. a "%" sign from the file. Following is the code I implemented but it has no effect. I think it is unable to read individual line or may be it just closes the file after reading from top to bottom. Please advice:

try
            {
                using (StreamReader Gcode = new StreamReader(path))
                {
                    string line;

                    while ((line = Gcode.ReadLine()) != null)
                    {
                        foreach (string line2 in File.ReadAllLines(path))
                        {
                            if (line2.Contains("%"))
                            {
                                line2.Replace("%", "");
                            }
                        }
                    }
                }
            }
Answers (33)