您的位置:首页 > 编程语言 > C#

分享一篇关于C#对文件操作的日志,方法很全

2011-01-08 11:52 701 查看
Code:

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();

}

/// <summary>

/// 方法名称:btnOpenFile_Click

/// 方法作用:打开文件方法一

/// 作者:心语

/// 完成时间:2010年5月25日19:49:29

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

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();

}

}

/// <summary>

/// 方法名称:btnOpenFile2_Click

/// 方法作用:打开文件方法二

/// 作者:心语

/// 完成时间:2010年5月25日19:55:31

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

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数组

byte[] bt = new byte[filestream.Length];

//调用read读取方法

filestream.Read(bt, 0, bt.Length);

//以Unicode编码方式显示文本

this.txtFileName.Text = Encoding.Unicode.GetString(bt);

}

}

/// <summary>

/// 方法名称:btnSaveFile_Click

/// 方法作用:保存文件

/// 作者:心语

/// 完成时间:2010年5月25日20:01:48

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

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();

}

}

/// <summary>

/// 方法名称:btnCopy_Click

/// 方法作用:复制文件

/// 作者:心语

/// 完成时间:2010年5月25日20:05:11

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

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);

}

}

/// <summary>

/// 方法名称:btnMove_Click

/// 方法作用:移动文件

/// 作者:心语

/// 完成时间:2010年5月25日20:11:22

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

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);

}

}

/// <summary>

/// 方法名称:btnDelete_Click

/// 方法作用:删除文件

/// 作者:心语

/// 完成时间:2010年5月25日20:14:46

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

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);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐