tags:

views:

204

answers:

1

I need to convert an .sdf file to a binary file. (I think with streamreader?) Then I need to convert this binary file back to .sdf. How do I do this?

+1  A: 

The file itself is already binary, so I'm assuming that you want to read the file into memory as binary. Give this a shot:

public byte[] ReadBinaryFile(string path)
{    
    byte[] data;

    using (System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
    {
        data = new byte[stream.Length];

        stream.Read(data, 0, data.Length);
    }

    return data;
}

If you want to write this byte[] back to the file, do this:

public void WriteBinaryFile(string path, byte[] data)
{
    using (System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Create))
    {
        stream.Write(data, 0, data.Length);
    }
}


Edit

I've updated this to encapsulate the logic into two functions.

Adam Robinson
thank's alot, how can I insert this in function that return the byte ?
Gold
What do you mean? All of the code you should need should be there for you...just have your function return the byte[] variable called data.
Adam Robinson
I need to transfer file between WebService and WinCE, and this is the best way. so if you can, to write for me the func that recive and the func that get the data ?. thenk's in advance
Gold
Edited. How you get the data between the two is up to you, but those two functions should be all you need. The first takes a path and returns the data as a byte[], and the second takes a path and a byte[] and writes the data to the file.
Adam Robinson
thank you very much
Gold