title: How to write a text file with specific positions with FileStream?
body: <p>I am trying to write a text file with this structure</p>
  <p><img src="https://i.stack.imgur.com/Xj54d.png" alt="enter image description here" /></p>
  <p>For this I am working with Seek() and Write(), but every time I want to add a new data it is overwritten, and in the end only one line is written to the file</p>
  <p><img src="https://i.stack.imgur.com/5QjQL.png" alt="enter image description here" /></p>
  <p>This is the code I am using</p>
  <pre class="lang-cs prettyprint-override"><code>private async void CreacionArchivoBCD(ArchivoPerfil contenidoPerfil, string nombreFinalBCD, bool checkPrevia)
  {
      var result = await ensayoDataProvider.ObtenerUexCapturador(checkPrevia, contenidoPerfil, codensSeleccionado.Id);
      var cant = System.Text.Encoding.UTF8.GetBytes(Convert.ToString(result.Count()));
  
  
      ////TextWriter archivoExportar = new StreamWriter(nombreFinalBCD);
      ////archivoExportar.Close();
  
      var file = new FileStream(nombreFinalBCD, FileMode.OpenOrCreate, FileAccess.Write);
      file.Seek(1, SeekOrigin.Begin);
      file.Write(cant, 0, cant.Length);
      byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
      file.Write(newline, 0, newline.Length);
  
      int nroMedAux = 0;
      long rutaAux = 0;
      var cont = 0;
      var bandera = 0;
      //var posicion = 0;
      foreach (var item in result)
      {
          var primerByte = System.Text.Encoding.UTF8.GetBytes(&quot;00&quot;);
          file.Seek(0, SeekOrigin.Begin);
          file.Write(primerByte, 0, primerByte.Length);
  
          if (bandera == 0)
          {
              nroMedAux = item.IdMed;
              rutaAux = item.Ruta;
              bandera = 1;
          }
  
          var tipo = item.GetType();
  
          if (nroMedAux != item.IdMed || rutaAux != item.Ruta)
          {
              nroMedAux = item.IdMed;
              rutaAux = item.Ruta;
              file.Write(newline, 0, newline.Length);
          }
  
          foreach (var pi in tipo.GetProperties())
          {
              var propName = pi.Name;
              var propValue = pi.GetValue(item, null)?.ToString();
              var propValueAux = propName == &quot;Campo&quot; ? item.GetType().GetProperty(&quot;Valor&quot;).GetValue(item, null) : propValue;
  
              if (propName == &quot;IdMed&quot; || propName == &quot;Valor&quot;)
              {
                  continue;
              }
  
              //TODO: buscar posicion y ancho por nombre campo
              var posicion = BuscarPosicion(propName, propValue, contenidoPerfil);
              //var ancho = BuscarAncho(propName, contenidoPerfil);
  
              Debug.WriteLine(propName);
              Debug.WriteLine(propValueAux);
              Debug.WriteLine(posicion);
  
              if (posicion == -1)
              {
                  continue;
              }
  
              var lacadena = propValue;
              var lacadenAux = propName == &quot;Campo&quot; ? propValueAux : propValue;
              var cadena = System.Text.Encoding.UTF8.GetBytes(lacadenAux.ToString());
              file.Seek(posicion, SeekOrigin.Begin);
              file.Write(cadena, 0, cadena.Length);
          }
      }
  
      file.Close();
  
  }
  </code></pre>