分享一篇关于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);
}
}
}
}
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);
}
}
}
}
相关文章推荐
- 分享一篇关于C#对文件操作的日志,方法很全
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- C#窗体操作SQL Server存储大文件方法分享
- c#文件助手类分享(读取文件内容 操作日志文件)
- C++中关于写日志文件Log的方法
- C#中常用的经典文件操作方法
- C#操作config文件的具体方法
- 一篇关于python对文件操作讲解的文章
- 分享一个格式化文件大小的 C# 方法(转)
- c++关于文件夹(文件)的相关操作_findfirst,_findnext和_findclose方法
- 【转】Asp.net(C#)对文件操作的方法(读取,删除,批量拷贝,删除...)
- Asp.net(C#)对文件操作的方法(读取,删除,批量拷贝,删除...)
- C#操作本地文件及保存文件到数据库的基本方法总结
- java读取文件内容的三种方法代码片断分享(java文件操作)
- 关于C#下文件操作,些许疑问!
- c# 文件操作 判断文件是否为空的方法
- Asp.net(C#)对文件操作的方法(读取,删除,批量拷贝,删除...)
- 公司内部的一篇关于dom方法的分享
- C++中关于写日志文件Log的方法
- C# 自动查找文件内容(正则使用、获取目录下所有文件、多线程、日志记录,文件操作)