分享一篇关于C#对文件操作的日志,方法很全
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- using System.IO;
-
- namespace NsFileOperate
- {
- public partial class FileOperate : Form
- {
- public FileOperate()
- {
- InitializeComponent();
- }
-
-
-
-
-
-
-
-
- private void btnOpenFile_Click(object sender, EventArgs e)
- {
-
- this.openFileDialog1.Title = "打开文件";
-
- this.openFileDialog1.InitialDirectory = "d://";
-
- this.openFileDialog1.Filter = "心语文件*.dxl|*.dxl|文本文件*.txt|*.txt|所有文件|*.*";
-
- this.openFileDialog1.FilterIndex = 1;
-
- this.openFileDialog1.RestoreDirectory = true;
-
- this.openFileDialog1.DefaultExt = ".txt";
-
- if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
- {
-
- string path = this.openFileDialog1.FileName;
-
- FileStream file = new FileStream(path, FileMode.Open);
-
- StreamReader reader = new StreamReader(file);
-
- this.txtFileName.Text = reader.ReadToEnd();
- }
- }
-
-
-
-
-
-
-
-
- private void btnOpenFile2_Click(object sender, EventArgs e)
- {
-
- OpenFileDialog open = new OpenFileDialog();
-
- open.InitialDirectory = "d://";
-
- open.Filter = "心语文件*.dxl|*.dxl|文本文件*.txt|*.txt|所有文件|*.*";
-
- open.FilterIndex = 1;
-
- open.RestoreDirectory = true;
-
- if (open.ShowDialog() == DialogResult.OK)
- {
-
- string path = open.FileName;
-
- FileStream filestream = new FileStream(path, FileMode.Open);
-
- byte[] bt = new byte[filestream.Length];
-
- filestream.Read(bt, 0, bt.Length);
-
- this.txtFileName.Text = Encoding.Unicode.GetString(bt);
- }
- }
-
-
-
-
-
-
-
-
- private void btnSaveFile_Click(object sender, EventArgs e)
- {
-
- SaveFileDialog save = new SaveFileDialog();
-
- save.InitialDirectory = "d://";
-
- save.Filter = "心语文件*.dxl|*.dxl|文本文件*.txt|*.txt";
-
- save.FilterIndex = 1;
-
- save.RestoreDirectory = true;
-
- if (save.ShowDialog() == DialogResult.OK)
- {
-
- string path = save.FileName;
-
- save.DefaultExt = ".txt";
-
- StreamWriter sw = new StreamWriter(path);
-
- sw.WriteLine(txtFileName.Text);
-
- sw.Close();
- }
- }
-
-
-
-
-
-
-
-
- private void btnCopy_Click(object sender, EventArgs e)
- {
-
- if (File.Exists("D://CopyToFile//Copy.txt") == true)
- {
- MessageBox.Show("目标文件夹已有此文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- else
- {
-
- File.Copy("D://Copy.txt", "D://CopyToFile//Copy.txt");
- MessageBox.Show("复制成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
-
-
-
-
-
-
-
-
- private void btnMove_Click(object sender, EventArgs e)
- {
- if (File.Exists("D://MoveToFile//Move.txt") == true)
- {
- MessageBox.Show("目标文件夹已有此文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- else
- {
-
- File.Move("D://Move.txt", "D://MoveToFile//Move.txt");
- MessageBox.Show("移动成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
-
-
-
-
-
-
-
-
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (Directory.Exists("D://Move.txt") == false)
- {
- MessageBox.Show("目标文件夹不存在此文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- else
- {
-
- Directory.Delete("D://Move.txt");
- MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- }
- }
-
----转载请注明出处http://www.cnblogs.com/JerryWang1991/ 谢谢!