C sharp - Modificar Archivos Binarios C#

 
Vista:

Modificar Archivos Binarios C#

Publicado por Heylin (1 intervención) el 09/05/2016 04:46:25
Necesito ayuda para poder modificar los registros dentro de un archivo en C#, debe desplegar los datos ingresados con anterioridad y permitirme modificarlos y luego guardar los cambios. Es urgente, agradeceré la ayuda

Estoy usando consola
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Modificar Archivos Binarios C#

Publicado por Edgar Giovanni Andrade Santamaría (10 intervenciones) el 19/05/2016 20:22:04
que onda... mira tengo este codigo espero y te sirva para modificar los archivos binarios. Suerte

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
ing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.IO; // No olvidar este using.
 
namespace Archivo_Binario
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine(@"Seleccione E, e, L, l, S o s.
E y e = Escribir.
L y l = Leer.
S y s = Salir.");
      string str = Console.ReadLine();
 
      while (true)
      {
        switch (str)
        {
          case "E":
          case "e": // Escritura.
 
            try
            {
              string fileName = "archivazo.txt";
              // data a ser guardada
              int[] data = { 0, 1, 2, 3, 4, 5 };
              FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
              BinaryWriter writer = new BinaryWriter(stream);
 
              for (int i = 0; i < data.Length; i++)
              {
                // números son guardados en formáto UTF-8 format (4 bytes)
                writer.Write(data[i]);
              }
 
              writer.Close();
              stream.Close();
            }
            catch
            {
              Console.WriteLine("Error.");
            }
            break;
 
          case "L": // Lectura.
          case "l":
 
            try
            {
              string fileName = "archivazo.txt";
              int letter = 0;
              FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
              BinaryReader reader = new BinaryReader(stream);
 
              while (letter != -1)
              {
                letter = reader.Read();
                if (letter != -1) Console.Write((char)letter);
              }
              reader.Close();
              stream.Close();
            }
            catch
            {
              Console.WriteLine("Error.");
            }
            break;
 
          case "S":
          case "s":
 
            break;
          default:
            Console.WriteLine("Selección inválida. Por favor, selecciona E, e, L, l, S o l.");
            break;
 
        }
      }
    }
  }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar