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();
}
/// <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);
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值