文件操作,加密,序列化

	
文件操作:
private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs;
            if (File.Exists("E:\\gui_down\\gui.txt"))
                 fs= File.Create("E:\\gui_down\\gui.txt");
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //
            File.Encrypt("E:\\gui_down\\gui.txt");//加密文件,表其他用户不能访问
            File.Decrypt("E:\\gui_down\\gui.txt");//解密文件
            System.Security.AccessControl.FileSecurity fileScrt = new System.Security.AccessControl.FileSecurity();
            File.SetAccessControl("E:\\gui_down\\gui.txt", fileScrt);//增加某个文件的访问权限,增加安全性
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileInfo file = new FileInfo("E:\\gui_down\\gui.txt");//实例化文件类,指定的文件路径作为参数
            file.Create();//文件创建
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string path;
            DialogResult result = openFileDialog1.ShowDialog();//打开对话框
            if (result == DialogResult.OK)
            {
                path = openFileDialog1.FileName;
                textBox1.Text = path;
            }
        }

        private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)//文件监视器
        {
            MessageBox.Show("E:\\gui_down\\gui.txt"+" changed");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FileStream fs = File.Create("E:\\gui_down\\feg.bin");//创建文件 返回的是文件流
            string line = "this is a test line.";
            UnicodeEncoding ue = new UnicodeEncoding();//编码类
            byte[] str = ue.GetBytes(line);//获取二进制流
            fs.Write(str, 0, str.Length);//用文件流对文件 读写
            fs.Close();//关闭文件流
        }

        private void button6_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("E:\\gui_down\\feg.bin", FileMode.Open,FileAccess.ReadWrite);
            byte[] str = new byte[1024];
            fs.Read(str, 0, str.Length);//将文件读取到流中
            fs.Close();
            UnicodeEncoding ue = new UnicodeEncoding();
            string line = ue.GetString(str);
            MessageBox.Show(line);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("E:\\gui_down\\gui.txt", FileMode.Append, FileAccess.Write);
            BufferedStream bs = new BufferedStream(fs);
            UnicodeEncoding ue = new UnicodeEncoding();
            byte[] str = new byte[1024];
            string line = "你好";
            str = ue.GetBytes(line);
            //加密,sha不可逆加密
            System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
            byte[] shastr = new byte[1024];
            shastr = sha.ComputeHash(str);
            bs.Write(shastr,0,shastr.Length);
            bs.Close();
            fs.Close();

        }

        private void button8_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("E:\\gui_down\\gui.txt", FileMode.Append, FileAccess.Write);
            BufferedStream bs = new BufferedStream(fs);//缓冲流
            StreamWriter sw = new StreamWriter(bs);//写入流
            string line = "你好"; 
         //   UnicodeEncoding ue = new UnicodeEncoding();
         // byte[] str = new byte[1024];
         //   str = ue.GetBytes(line);
            sw.WriteLine(line);
            sw.Close();
            bs.Close();
            fs.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("E:\\gui_down\\g.txt",FileMode.Open,FileAccess.Write);
            DeflateStream deflate = new DeflateStream(fs,CompressionMode.Compress);//deflate文件压缩
            System.IO.Compression.GZipStream gzip = new GZipStream(fs, CompressionMode.Compress);//gzip文件压缩

        }

        private void button10_Click(object sender, EventArgs e)
        {
            ArrayList at = new ArrayList();
            for(int i = 0;i<100;i++)
              at.Add(i);
            FileStream fs = File.Create("E:\\gui_down\\g1.txt");
            //二进制序列化,目的是将内存中的内容存入本地
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs,at);
            fs.Close();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            FileStream fs = File.Open("E:\\gui_down\\g1.txt", FileMode.Open);
            BinaryFormatter bin = new BinaryFormatter();
            ArrayList list;
            list = (ArrayList)bin.Deserialize(fs);
            fs.Close();
            MessageBox.Show(list.Count.ToString());
            MessageBox.Show(list[2].ToString());
        }

        private void button12_Click(object sender, EventArgs e)
        {
            FileStream fs = File.Create("E:\\gui_down\\g2.xml");
            ArrayList at = new ArrayList();
            for (int i = 0; i < 100; i++)
                at.Add(i);
            System.Runtime.Serialization.Formatters.Soap.SoapFormatter soap = new SoapFormatter();//soap序列化
            soap.Serialize(fs, at);
            fs.Close();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            FileStream fs = File.Create("E:\\gui_down\\g3.xml");
            ArrayList at = new ArrayList();
            for (int i = 0; i < 100; i++)
                at.Add(i);
            System.Xml.Serialization.XmlSerializer xml= new XmlSerializer(typeof(ArrayList));//xml序列化
            xml.Serialize(fs,at);
            fs.Close();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Employee emp = new Employee("gui", 21, 20000);
            FileStream fs = File.Create("E:\\gui_down\\g4.xml");
            SoapFormatter soap = new SoapFormatter();//必须对指定可序列化的对象而言,若是自己创建的类,可以在类开头申明为可序列化
            soap.Serialize(fs, emp);//序列化只对指定的类的数据成员进行
            fs.Close();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Employee emp;
            FileStream fs = File.Open("E:\\gui_down\\g4.xml", FileMode.Open);
            SoapFormatter soap = new SoapFormatter();
            emp = (Employee)soap.Deserialize(fs);//反序列化 除了要知道文件路径以外,还要知道被序列化的类型(Employee)
            fs.Close();
            MessageBox.Show(emp.Name);
            //对于soap序列化的问题,可以在设计方法上用接口实现仅被序列化的类,使得引用中其他的类不公开
        }
[SerializableAttribute]//可序列化
    public class Employee
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int age;
        [NonSerialized]//该数据成员部能被序列化
        private int saray;
        public Employee(string n,int a,int s)
        {
         this.name =n;
            this.age = a;
            this.saray = s;
        }

    }

你可能感兴趣的:(xml,Security,SOAP)