CSharpFeeds - All your C# feeds in one place.

Sponsors

Sunday, February 25, 2007

Serialize files to base64 strings

by post@madskristensen.dk (Mads Kristensen) via .NET Slave on 2/25/2007 11:21:23 PM

Today, I needed to store images in XML files so I had to serialize an image file into text so it could be written in the XML file. Of course, it should also be deserialized back to an image again by reading from the XML file. The result is the following two methods.

The first method serializes a file on disk and returns the base64 string representation.

public static string Serialize(string fileName)
{
 using (FileStream reader =new FileStream(fileName, FileMode.Open))
 {
  byte[] buffer =newbyte[reader.Length];
  reader.Read(buffer, 0, (int)reader.Length);
  return Convert.ToBase64String(buffer);
 }
}

The next deserializes a base64 string and writes it to the disk.

public static void DeSerialize(string fileName, string serializedFile)
{
 using (System.IO.FileStream reader = System.IO.File.Create(fileName))
 {
  byte[] buffer = Convert.FromBase64String(serializedFile);
  reader.Write(buffer, 0, buffer.Length);
 }
}

email it!bookmark it!digg it!

Original Post: Serialize files to base64 strings

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us