Copy certain lines from one text file to another I need to open a file, read a line and if that line respect some conditions write it to another file, the line that I'm reading is a normal ASCII string representig HEX values and I need to paste it into a new file as HEX values and not as ASCII string.

What I have is this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    byte[] arrayByte = { 0x00 };
    var linesToKeep = File.ReadLines(fileName).Where(l =&gt; l.Contains(":10"));
    foreach (string line in linesToKeep)
    {

        string partialA = line.Substring(9);
        string partialB = partialA.Remove(partialA.Length - 2);
        arrayByte = ToByteArray(partialB);
        using (var stream = new FileStream(fileName+"_gugggu", FileMode.OpenOrCreate))
        {
            FileInfo file = null;
            file = new FileInfo(fileName + "_gugggu");
            stream.Position = file.Length;
            stream.Write(arrayByte, 0, arrayByte.Length);
        }
    }
}
public static byte[] ToByteArray(String HexString)
{
    int NumberChars = HexString.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i &lt; NumberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
    }
    return bytes;
}


This method is doing what I need but it takes ages to finish, the original files have roughly 70000 lines... Is there a better way to do that in order to increase speed?

EDIT
SOURCE FILE EXAMPLE:

:106AD00000000000000000000000000000000000B6
:106AE00000000000000000000000000000000000A6
:106AF0000000000000000000000000000000000096
:106B00000000000000000000000000000000000085
:106B10000000000000000000000000000000000075
:106B20000000000000000000000000000000000065
:106B30000000000000000000000000000000000055
:106B40000000000000000000000000000000000045
:106B50000000000000000000000000000000000035
:106B60000000000000000000000000000000000025
It is OK to use File.ReadLines(...) because it actually is shortcut to StreamReader.ReadLine() (Not to be confused with File.ReadAllLines()).



I wonder why you reopen the same file for each line you want to save. I would do something like this:

  byte[] arrayByte = { 0x00 };

  using (var stream = new FileStream(fileName + "_gugggu", FileMode.Create))
  {
    foreach (string line in File.ReadLines(fileName).Where(l =&gt; l.Contains(":10")))
    {
      ...


This may be the real bottleneck.



I think 

      string partialA = line.Substring(9);
      string partialB = partialA.Remove(partialA.Length - 2);


is the same as:

      string subString = line.Substring(9, line.Length - 11); // 11 = 9 + 2




All  in all it could be changed to :

  byte[] arrayByte = { 0x00 };

  using (var stream = new FileStream(fileName + "_gugggu", FileMode.Create))
  {
    foreach (string line in File.ReadLines(fileName).Where(l =&gt; l.Contains(":10")))
    {
      string subString = line.Substring(9, line.Length - 11);
      arrayByte = ToByteArray(subString);
      stream.Write(arrayByte, 0, arrayByte.Length);
    }
  }