将文件用base64编码保存成字符串,并从字符串中还原。

        //

        //  using System;

        //  using System.IO;

        //

 

        ///

        /// Temporary storage.

        ///

        private string buffer;

 

 

        ///

        /// Write base64 string to temp storage.

        ///

        private void base64Encode( string fromFile )

        {

            FileStream fileStream = File.Open(fromFile , FileMode.Open);

 

            byte[] buffer = new byte[fileStream.Length];

 

            fileStream.Read(buffer, 0, buffer.Length);

 

            fileStream.Close();

 

            this.buffer = Convert.ToBase64String(buffer);

        }

 

        ///

        /// Read base64 string from temp storage and save to file.

        ///

        private void base64Decode( string toFile )

        {

            FileStream fileStream = new FileStream(toFile, FileMode.Create);

 

            byte[] buffer = Convert.FromBase64String(this.buffer);

 

            fileStream.Write(buffer, 0, buffer.Length);

 

            fileStream.Close();

        }

 

        ///

        /// Form event.

        ///

        /// "sender">

        /// "e">

        private void button1_Click(object sender, EventArgs e)

        {

            this.base64Encode("C://plastic.bmp");

        }

 

        ///

        /// Form event.

        ///

        /// "sender">

        /// "e">

        private void button2_Click(object sender, EventArgs e)

        {

            this.base64Decode("C://tmp.bmp");

        }

你可能感兴趣的:(.NET)