1.文件复制
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
文件复制namespace 文件操作01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textBox1.Text = ofd.FileName; } } private void button2_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textBox3.Text = fbd.SelectedPath; } } private void button3_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text) || string.IsNullOrWhiteSpace(textBox3.Text)) { return; } double Num=Convert.ToDouble(comboBox1.SelectedItem); byte[] bs = new byte[(int)(Num * 1024 * 1024)]; FileStream read = new FileStream(textBox1.Text.Trim(), FileMode.Open, FileAccess.Read); FileStream write = new FileStream(Path.Combine(textBox3.Text.Trim(), textBox2.Text).Trim(), FileMode.Create, FileAccess.Write); using (read) { using (write) { int count = 0; while((count=read.Read(bs,0,bs.Length))!=0) { write.Write(bs, 0, count); } } } MessageBox.Show("复制成功"); } } }
2.文件加密与解密
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
文件加密namespace 文件加密与解密 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textBox1.Text = ofd.FileName; } } private void button4_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if(fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textBox3.Text = fbd.SelectedPath; } } private void button2_Click(object sender, EventArgs e) { FileStream read = new FileStream(textBox1.Text.Trim(), FileMode.Open, FileAccess.Read); FileStream write = new FileStream(Path.Combine(textBox3.Text.Trim(), textBox2.Text.Trim()), FileMode.Create, FileAccess.Write); using (read) { using (write) { int count = 0; byte[] bs = new byte[10000]; while ((count = read.Read(bs, 0, bs.Length)) != 0) { for (int i = 0; i < count; i++) { bs[i] = (byte)(bs[i] - 1); } write.Write(bs, 0, count); } } } MessageBox.Show("加密成功"); } private void button3_Click(object sender, EventArgs e) { FileStream read = new FileStream(textBox1.Text.Trim(), FileMode.Open, FileAccess.Read); FileStream write = new FileStream(Path.Combine(textBox3.Text.Trim(), textBox2.Text.Trim()), FileMode.Create, FileAccess.Write); using (read) { using (write) { int count = 0; byte[] bs = new byte[10000]; while ((count = read.Read(bs, 0, bs.Length)) != 0) { for (int i = 0; i < count; i++) { bs[i] = (byte)(bs[i] + 1); } write.Write(bs, 0, count); } } } MessageBox.Show("解密成功"); } private void button5_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } } }
3序列化
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
序列化namespace 序列化 { class Program { static void Main(string[] args) { List<person> personlist = new List<person>(); personlist.Add(new person("张三1",'男',18)); personlist.Add(new person("张三2", '男', 18)); personlist.Add(new person("张三3", '男', 18)); personlist.Add(new person("张三4", '男', 18)); personlist.Add(new person("张三5", '男', 18)); personlist.Add(new person("张三6", '男', 18)); FileStream write = new FileStream(@"C:\Users\Avraber\Desktop\新建文件夹\4.txt", FileMode.Create, FileAccess.Write); using (write) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(write,personlist); } FileStream read = new FileStream(@"C:\Users\Avraber\Desktop\新建文件夹\4.txt", FileMode.Open, FileAccess.Read); using (read) { BinaryFormatter bf = new BinaryFormatter(); personlist= (List<person>)bf.Deserialize(read); } } } [Serializable] class person { public person(string name, char gender, int age) { this.name = name; this.age = age; this.gender = gender; } string name; public string Name { get { return name; } set { name = value; } } char gender; public char Gender { get { return gender; } set { gender = value; } } int age; public int Age { get { return age; } set { age = value; } } } }