C#中I/O操作
来源:互联网 发布:淘宝商城儿童睡袋 编辑:程序博客网 时间:2024/05/20 04:14
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 TextFile
{
public partial class FrmFile : Form
{
int Count = 0;
public FrmFile()
{
InitializeComponent();
}
//写入文件
private void btnWrite_Click(object sender, EventArgs e)
{
//【1】创建文件流
FileStream fs = new FileStream("F:\\myfile.txt",FileMode.Create);
//【2】创建写入器
StreamWriter sw = new StreamWriter(fs);
//【3】以流的方式写入数据
sw.Write(this.txtFile.Text.Trim());
//【4】关闭写入器
sw.Close();
//【5】关闭文件流
fs.Close();
}
//读取文件
private void btnRead_Click(object sender, EventArgs e)
{
//【1】创建文件流
FileStream fs = new FileStream("F:\\myfile.txt",FileMode.Open);
//【2】创建读取器
// StreamReader sr = new StreamReader(fs,Encoding.Default); //encoding,default 读取默认的编码方法
StreamReader sr = new StreamReader(fs);
//【3】以流的方式读取数据
this.txtFile.Text=sr.ReadToEnd();
//【4】关闭读取器
sr.Close();
//【5】关闭文件流
fs.Close();
}
//模拟写日志
private void button1_Click(object sender, EventArgs e)
{
Count++;
//创建文件流
FileStream fs = new FileStream("F:\\myfile.txt",FileMode.Append);
//创建写入器
StreamWriter sr = new StreamWriter(fs);
//以流的方式逐行写入数据
sr.WriteLine(DateTime.Now.ToString()+":次数" + "{0}",Count);
//关闭写入器
sr.Close();
//关闭文件流
fs.Close();
}
//删除文件
private void btnDelete_Click(object sender, EventArgs e)
{
File.Delete(this.txtThisFile.Text.Trim());
}
//复制文件
private void btnCopy_Click(object sender, EventArgs e)
{
//若目标文件已经存在,则删除该文件
if(File.Exists(this.txtOtherFile.Text.Trim()))
{
File.Delete(this.txtOtherFile.Text.Trim());
}
else
{
//判断源文件是否存在
if (!File.Exists(this.txtThisFile.Text.Trim()))
{
MessageBox.Show("不存在源文件", "错误提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
File.Copy(this.txtThisFile.Text.Trim(), this.txtOtherFile.Text.Trim());
}
}
}
//移动文件
private void btnMove_Click(object sender, EventArgs e)
{
//若目标文件已经存在,则删除该文件
if (File.Exists(this.txtOtherFile.Text.Trim()))
{
File.Delete(this.txtOtherFile.Text.Trim());
}
else
{
//判断源文件是否存在
if (!File.Exists(this.txtThisFile.Text.Trim()))
{
MessageBox.Show("不存在源文件,移动失败!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
File.Move(this.txtThisFile.Text.Trim(), this.txtOtherFile.Text.Trim());
}
}
}
//显示指定目录下的所有文件
private void btnShowAllFile_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("F:\\图书");
this.txtFile.Clear();
foreach (string item in files)
{
this.txtFile.Text+=item+"\r\n";
}
}
//显示指定目录下的文件
private void btnShowOneFile_Click(object sender, EventArgs e)
{
string[] dirs = Directory.GetDirectories("F:\\图书");
this.txtFile.Clear();
foreach (string item in dirs)
{
this.txtFile.Text += item + "\r\n";
}
}
//在指定目录下创建一个子目录
private void btnNewOneFile_Click(object sender, EventArgs e)
{
Directory.CreateDirectory("F:\\图书\\包子林");
}
//删除指定目录下的所有目录和文件
private void btnDeleteAllFile_Click(object sender, EventArgs e)
{
//Directory.Delete("F:\\图书\\包子林"); //删除包子林文件夹时出错--使用该方法,目录必须为空
DirectoryInfo dir = new DirectoryInfo("F:\\图书\\包子林");
dir.Delete(true);
}
}
}
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 TextFile
{
public partial class FrmFile : Form
{
int Count = 0;
public FrmFile()
{
InitializeComponent();
}
//写入文件
private void btnWrite_Click(object sender, EventArgs e)
{
//【1】创建文件流
FileStream fs = new FileStream("F:\\myfile.txt",FileMode.Create);
//【2】创建写入器
StreamWriter sw = new StreamWriter(fs);
//【3】以流的方式写入数据
sw.Write(this.txtFile.Text.Trim());
//【4】关闭写入器
sw.Close();
//【5】关闭文件流
fs.Close();
}
//读取文件
private void btnRead_Click(object sender, EventArgs e)
{
//【1】创建文件流
FileStream fs = new FileStream("F:\\myfile.txt",FileMode.Open);
//【2】创建读取器
// StreamReader sr = new StreamReader(fs,Encoding.Default); //encoding,default 读取默认的编码方法
StreamReader sr = new StreamReader(fs);
//【3】以流的方式读取数据
this.txtFile.Text=sr.ReadToEnd();
//【4】关闭读取器
sr.Close();
//【5】关闭文件流
fs.Close();
}
//模拟写日志
private void button1_Click(object sender, EventArgs e)
{
Count++;
//创建文件流
FileStream fs = new FileStream("F:\\myfile.txt",FileMode.Append);
//创建写入器
StreamWriter sr = new StreamWriter(fs);
//以流的方式逐行写入数据
sr.WriteLine(DateTime.Now.ToString()+":次数" + "{0}",Count);
//关闭写入器
sr.Close();
//关闭文件流
fs.Close();
}
//删除文件
private void btnDelete_Click(object sender, EventArgs e)
{
File.Delete(this.txtThisFile.Text.Trim());
}
//复制文件
private void btnCopy_Click(object sender, EventArgs e)
{
//若目标文件已经存在,则删除该文件
if(File.Exists(this.txtOtherFile.Text.Trim()))
{
File.Delete(this.txtOtherFile.Text.Trim());
}
else
{
//判断源文件是否存在
if (!File.Exists(this.txtThisFile.Text.Trim()))
{
MessageBox.Show("不存在源文件", "错误提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
File.Copy(this.txtThisFile.Text.Trim(), this.txtOtherFile.Text.Trim());
}
}
}
//移动文件
private void btnMove_Click(object sender, EventArgs e)
{
//若目标文件已经存在,则删除该文件
if (File.Exists(this.txtOtherFile.Text.Trim()))
{
File.Delete(this.txtOtherFile.Text.Trim());
}
else
{
//判断源文件是否存在
if (!File.Exists(this.txtThisFile.Text.Trim()))
{
MessageBox.Show("不存在源文件,移动失败!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
File.Move(this.txtThisFile.Text.Trim(), this.txtOtherFile.Text.Trim());
}
}
}
//显示指定目录下的所有文件
private void btnShowAllFile_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("F:\\图书");
this.txtFile.Clear();
foreach (string item in files)
{
this.txtFile.Text+=item+"\r\n";
}
}
//显示指定目录下的文件
private void btnShowOneFile_Click(object sender, EventArgs e)
{
string[] dirs = Directory.GetDirectories("F:\\图书");
this.txtFile.Clear();
foreach (string item in dirs)
{
this.txtFile.Text += item + "\r\n";
}
}
//在指定目录下创建一个子目录
private void btnNewOneFile_Click(object sender, EventArgs e)
{
Directory.CreateDirectory("F:\\图书\\包子林");
}
//删除指定目录下的所有目录和文件
private void btnDeleteAllFile_Click(object sender, EventArgs e)
{
//Directory.Delete("F:\\图书\\包子林"); //删除包子林文件夹时出错--使用该方法,目录必须为空
DirectoryInfo dir = new DirectoryInfo("F:\\图书\\包子林");
dir.Delete(true);
}
}
}
阅读全文
0 0
- C#中I/O操作
- C# I/O操作
- c# I/O相关操作
- C# I/O操作 笔记
- C#笔记28:I/O操作
- C#对象的I/O操作
- Java中I/O操作总结
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总(1)
- C# 路径/文件/目录/I/O常见操作汇总收藏
- C#路径/文件/目录/I/O常见操作汇总
- C#路径/文件/目录/I/O常见操作汇总
- linux上anaconda的卸载
- 贪吃蛇源代码
- Logistic回归
- jsp页面转发到servlet
- 自定义软键盘,随机数字位置键盘
- C#中I/O操作
- UGUI研究院之新手引导事件上下分离
- Android 常用测试接口 视频,天气等
- 51nod 1605-棋盘游戏(博弈)
- 自学nginx(一): nginx的快速安裝
- spark-streaming 编程(二) word count单词计数统计
- 使用Gson解析复杂的泛型嵌套泛型数据结构
- 网络爬虫思路
- PHP中echo(),print(),print_r()的区别及打印函数