我有一个顾客信息的文件,但是为了不让用户随意乱改,必须做成二进制文件,
当然后面还要用到这个文件,我怎么转换成二进制文件,并且把它转化回来

解决方案 »

  1.   

    所有文件都是以二进制的方式存的,因此文本文件也不例外。只不过对于文本文件,C#提供专门的读写方式,只是com替你做了转换而已。如果你是想对文本文件内容做转换,有很多加密算法,例如base,rsa等等,
    提供两个
    http://blog.csdn.net/Knight94/archive/2006/07/04/876493.aspx
    http://blog.csdn.net/Knight94/archive/2006/07/03/868951.aspx
      

  2.   

    BinaryReader
    BinaryWriter
    这两个类可以帮你.
    是以二进制读写入文件的
      

  3.   

    BinaryWriter写入文件能看到内容的,和txt一样
      

  4.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using Microsoft.VisualBasic;namespace TextIO
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                char[] chars;            UncodeToUtf8 uc2u8 = new UncodeToUtf8(richTextBox1.Text);
                SaveText st = new SaveText(richTextBox1.Text);            //保存为U8格式的文本文件
                FileStream fs1 = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
                fs1.Write(uc2u8.content, 0, uc2u8.content.Length);
                fs1.Flush();            //对字节数组进行反编码,UTF8解码
                UTF8Encoding u8Code = new UTF8Encoding();
                chars = u8Code.GetChars(uc2u8.content);
                MessageBox.Show(new StringBuilder().Insert(0, chars).ToString());            //保存为PC格式的文本文件
                FileStream fs2 = new FileStream(textBox2.Text, FileMode.OpenOrCreate);
                fs2.Write(st.content, 0, st.content.Length);
                fs2.Flush();            //对字节数组进行反编码,PC解码
                Encoding defaultCode = Encoding.Default;
                chars = defaultCode.GetChars(st.content);
                MessageBox.Show(new StringBuilder().Insert(0, chars).ToString());
            }        private void button2_Click(object sender, EventArgs e)
            {
                //将richTextBox1中读取到的数据进行编码
                UncodeToUtf8 uc2u8 = new UncodeToUtf8(richTextBox1.Text);
                SaveText st = new SaveText(richTextBox1.Text);            FileStream fs1 = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
                BinaryWriter bWriter1 = new BinaryWriter(fs1);
                MessageBox.Show(uc2u8.content.Length.ToString());
                //将byte转换成int32之后保存为二进制文件
                for (int i = 0; i < uc2u8.content.Length; i++)
                {
                    bWriter1.Write(Convert.ToInt32(uc2u8.content[i]));
                }
                bWriter1.Flush();
                bWriter1.Close();
                fs1.Close();
            }        private void button3_Click(object sender, EventArgs e)
            {
                FileStream fs1 = new FileStream(textBox3.Text, FileMode.OpenOrCreate);
                BinaryReader bRead = new BinaryReader(fs1);
                ArrayList aList = new ArrayList();
                //读取二进制文件的数据,存放在ArrayList集合中
                while (bRead.PeekChar() != -1)
                {
                    aList.Add(bRead.ReadInt32());
                }
                //创建byte数组
                byte[] bytes = new byte[aList.Count];
                //将ArrayList中的int32数据转换成byte后,存放在bytes数组中
                for (int i = 0; i < bytes.Length; i++ )
                {
                    bytes[i] = Convert.ToByte((int)aList[i]);
                }
                //将bytes数组转换成u8格式的字符串
                string s = new UTF8Encoding(true).GetString(bytes);
                MessageBox.Show(s);
                bRead.Close();
                fs1.Close();
            }
        }    public class SaveText
        {
            public byte[] content;        public SaveText(string text)
            {
                //使用默认的编码方式对文本进行编码
                Encoding encode = Encoding.Default;
                content = encode.GetBytes(text);
            }
        }    public class UncodeToUtf8
        {
            public byte[] content;        public UncodeToUtf8(string text)
            {
                //使用UTF8对文本进行编码
                content = new UTF8Encoding(true).GetBytes(text);
            }
        }
    }
      

  5.   

    http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B307010