Return to Snippet

Revision: 6109
at April 28, 2008 19:45 by rengber


Initial Code
private void btnEncode_Click(object sender, EventArgs e)
{
  if (!string.IsNullOrEmpty(txtInFile.Text))
  {
    FileStream fs = new FileStream(txtInFile.Text, 
                                   FileMode.Open, 
                                   FileAccess.Read);
    byte[] filebytes = new byte[fs.Length];
    fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
    string encodedData = 
        Convert.ToBase64String(filebytes,                 
                               Base64FormattingOptions.InsertLineBreaks);
    txtEncoded.Text = encodedData; 
  }
}

private void btnDecode_Click(object sender, EventArgs e)
{
  if (!string.IsNullOrEmpty(txtOutFile.Text))
  {
    byte[] filebytes = Convert.FromBase64String(txtEncoded.Text);
    FileStream fs = new FileStream(txtOutFile.Text, 
                                   FileMode.CreateNew, 
                                   FileAccess.Write, 
                                   FileShare.None);
    fs.Write(filebytes, 0, filebytes.Length);
    fs.Close(); 
  }
}

Initial URL


Initial Description
Useful for sending files past filters via email.

Initial Title
Base64 Encode or Decode a File

Initial Tags


Initial Language
C#