一、对文件的创建(create)
1 private void button1_Click(object sender, EventArgs e) 2 { 3 File.Create(@"F:\\QQPCmgr\\Desktop\\新创的.txt"); 4 MessageBox.Show("创建成功!","消息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk); 5 }
二、对文件的删除(delete)
1 private void button2_Click(object sender, EventArgs e) 2 { 3 File.Delete("F:\\QQPCmgr\\Desktop\\新创的.txt"); 4 MessageBox.Show("删除成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 5 }
三、对文件的复制(copy)(exists判断文件是否存在)
1 private void button4_Click(object sender, EventArgs e) 2 { 3 if (File.Exists("F:\\QQPCmgr\\Desktop\\dos.txt")==false)//Exists判断文件是否存在 4 { 5 File.Copy("C:\\dos.txt", "F:\\QQPCmgr\\Desktop\\dos.txt"); 6 MessageBox.Show("复制成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 7 } 8 else 9 { 10 MessageBox.Show("该文件已存在!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error); 11 } 12 }
四、对文件的剪切(move)
1 private void button3_Click(object sender, EventArgs e) 2 { 3 File.Move("D:\\8989.txt", "F:\\QQPCmgr\\Desktop\\8989.txt"); 4 MessageBox.Show("剪切成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 5 }
五、txt文本的读取(read)
1 private void button5_Click(object sender, EventArgs e) 2 { 3 //按字节读取,读取的内容放到byte数组中 4 //byte[] buffer = File.ReadAllBytes(@"F:\\QQPCmgr\\Desktop\\8989.txt"); 5 //string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length);//解码成字符串 6 7 //直接按文本读取 8 string str =File.ReadAllText(@"F:\\QQPCmgr\\Desktop\\8989.txt",Encoding.Default); 9 this.textBox1.Text = str; 10 }
六、txt文本的写入(write)(会覆盖以前的内容)
1 private void button6_Click(object sender, EventArgs e) 2 { 3 //string xie = this.textBox1.Text; 4 //byte[] buffer = Encoding.Default.GetBytes(xie); 5 //File.WriteAllBytes(@"F:\\QQPCmgr\\Desktop\\new.txt", buffer); 6 7 string str = this.textBox1.Text; 8 File.WriteAllText(@"F:\\QQPCmgr\\Desktop\\new.txt", str); 9 MessageBox.Show("写入成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 10 }
七、txt的追加写入(append)(不会覆盖之前写入的内容)
1 private void button7_Click(object sender, EventArgs e) 2 { 3 string str = this.textBox1.Text; 4 File.AppendAllText(@"F:\\QQPCmgr\\Desktop\\new.txt", str); 5 MessageBox.Show("再写入成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 6 }
ps:大概都差不多的,就几个单词的变化
时间: 2024-11-06 05:41:08