saving values of an array plus values of other variables holds reference to it into the harddrive

269 views Asked by At

I have a byte[] array from a bitmap. I could save it to a file via:

File.WriteAllBytes("path&fileName", byte[]arrayName);

There are other variables that reference this byte[] array, such as its name, its original location, etc. In short, how do I collectively save data or group a few variables into a set for the sake of saving bitmap reference values?

(data = byte[], name=string, loctionX = int, locationY = int, Width=int ....etc)

Does this task belong to the database field only or is there another way? For example, can I use XML or linq? (I am trying to guess) (: I'm trying to avoid involving databases (SqlDatasource connection ...etc) in my project.

1

There are 1 answers

1
Marc Gravell On BEST ANSWER

You should declare a POCO for the values you want to store and serialize it. For example:

public class Foo {
    public byte[] Data {get;set;}
    public string Name {get;set;}
    public int LocationX {get;set;}
    // ... etc
}

Now pick any serializer; XmlSerializer, DataContractSerializer, protobuf-net, BinaryFormatter, JSON.NET - any would do; and use that to read/write the instance from/to disk. For example:

Foo obj = ...
using(var file = File.Create(path)) {
    var ser = new XmlSerializer(typeof(Foo));
    ser.Serialize(file, obj);
}