C sharp - Fichero de Objectos.

 
Vista:

Fichero de Objectos.

Publicado por nestor (1 intervención) el 18/02/2007 23:43:17
Saludos a todos, denme una mano con este problema,pf.

El problem es que estoy tratando de transferir una aplicacion un poco vieja que tenía en C++ hacia C# y me ha surgido este problema... Los datos yo lo guardo en un fichero binariom y para localizar un dato lo hago de esta forma:

// esto me permite moverme hacia el registro 6 del fichero.
fichero.seekg(5*sizeof(stRegistro), ios::beg);

// aqui leo es registro no.6
fichero.read(®, sizeof(stRegistro));

el problema es cómo logro esto en C#..., lo siguiente me da errores:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
[Serializable]
class Worker
{
private Int32 codigo;
protected String nombre;

public Worker(Int32 codigo, String nombre)
{
this.codigo = codigo;
this.nombre = nombre;
}

public Int32 Codigo
{
get { return this.codigo; }
set
{
if (value <= 0)
throw new ArgumentException("Codigo incorrecto");

this.codigo = value;
}
}

public String Nombre
{
get { return this.nombre; }
set
{
if (value.Length == 0)
throw new ArgumentException("Nombre incorrecto");

this.nombre = value;
}
}
}

class Program
{
static void Main(string[] args)
{
Worker w = new Worker(32, "Nombre");

FileStream fs = File.Create ("datos.bin");
IFormatter bw = new BinaryFormatter();

bw.Serialize(fs, w);

/* Esta es la parte donde no funciona */

// localiza el registro no.6
fs.Seek(5*sizeof(Worker), SeekOrigin.Begin);
// fs.Seek(6, SeekOrigin.Begin);

w = null;
w = (Worker)bw.Deserialize(fs);
fs.Close();

Console.WriteLine("Codigo: {0}\nNombre: {1}",w.Codigo, w.Nombre);

Console.ReadKey();
}
}
}

Que es lo incorrecto??

saludos.
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