C# 文件和文件夹的基本操作
文件流,文件夹的基本操作。有一些异常的判断没有加,实际中可以加一些异常的判断。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; // [1] 引入文件操作命名空间
namespace FilesOperate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
// 写文件
private void btnWriteFile_Click(object sender, EventArgs e)
{
//[1]创建文件流
FileStream fs = new FileStream(@"./huvjie.info", FileMode.Create);// 创建,有则覆盖
//[2]创建写入器
StreamWriter sw = new StreamWriter(fs);
//[3]以流的方式写入数据
sw.Write(this.txtContents.Text.Trim());
//[4]关闭写入器
sw.Close();
//[5]关闭文件流
fs.Close();
txtContents.Clear();
System.Diagnostics.Process.Start("notepad", @"./huvjie.info");
}
// 读取文件
private void btnReadFile_Click(object sender, EventArgs e)
{
try {
// [1]创建文件流
FileStream fs = new FileStream(@"./huvjie.info", FileMode.Open);// 打开的方式
// [2]创建读取器
StreamReader sr = new StreamReader(fs);
// [3]以流的方式读取数据
txtContents.Text = sr.ReadToEnd();
// [4]关闭读取器
sr.Close();
// [5]关闭文件流
fs.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "提示");
}
}
// 增加一个时间,模拟一下日志的记录,操作步骤和写入文件一样
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"./log.log", FileMode.Append);// 这里是追加,无文件会创建文件
StreamWriter sw = new StreamWriter(fs);
sw.Write("操作成功!" + DateTime.Now.ToString() + "\r\n");
sw.Close();
fs.Close();
//System.Diagnostics.Process[] pr = System.Diagnostics.Process.GetProcessesByName("notepad");
//if (pr.Length != 0) {
// pr[0].Kill();
//}
System.Diagnostics.Process.Start("notepad", @"./log.log");
}
// 删除文件
private void btnDelFile_Click(object sender, EventArgs e)
{
File.Delete(@"./log.log");
File.Delete(@"./huvjie.info");
File.Delete(@"./log_copy.log");
File.Delete(@"./huvjie_copy.info");
}
// 复制文件
private void button2_Click(object sender, EventArgs e)
{
// 判断下要复制过去的路径下有不有存在的文件,如果有复制会出现问题,这里把它删掉了。
if (File.Exists(@"./log_copy.log") || File.Exists(@"./huvjie_copy.info")) {
File.Delete(@"./log_copy.log");
File.Delete(@"./huvjie_copy.info");
}
File.Copy(@"./log.log", @"./log_copy.log");
File.Copy(@"./huvjie.info", @"./huvjie_copy.info");
System.Diagnostics.Process.Start("notepad", @"./log_copy.log");
System.Diagnostics.Process.Start("notepad", @"./huvjie_copy.info");
}
// 移动文件和复制文件差不多
private void btnMoveFile_Click(object sender, EventArgs e)
{
// 判断下要复制过去的路径下有不有存在的文件,如果有移动会出现问题,这里把它删掉了。
if (File.Exists(@"./log_copy.log") || File.Exists(@"./huvjie_copy.info")) {
File.Delete(@"./log_copy.log");
File.Delete(@"./huvjie_copy.info");
}
File.Move(@"./log.log", @"./0/log_copy.log");
File.Move(@"./huvjie.info", @"./0/huvjie_copy.info");
System.Diagnostics.Process.Start("notepad", @"./0/log_copy.log");
System.Diagnostics.Process.Start("notepad", @"./0/huvjie_copy.info");
}
//显示当前目录下的文件
private void btnGetAllDir_Click(object sender, EventArgs e)
{
//string[] files = Directory.GetDirectories(@".");
string[] files = Directory.GetFiles(@".");
this.txtContents.Clear();
foreach (string item in files) {
txtContents.Text = item + "\r\n";
}
}
// 这个和上面差不多
private void button2_Click_1(object sender, EventArgs e)
{
string[] files = Directory.GetDirectories(@".");
//string[] files = Directory.GetFiles(@"./");
this.txtContents.Clear();
foreach (string item in files) {
txtContents.Text = item + "\r\n";
}
}
// 创建一个文件夹
private void button3_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(@"./文件夹");
}
// 删除指定路径下所有目录和文件
private void btnDelAllDirOrFiles_Click(object sender, EventArgs e)
{
DirectoryInfo dirinfo = new DirectoryInfo(@".\0");
dirinfo.Delete(true);
//Directory.Delete(""); // 这个只能删除为空的目录
}
}
}
效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了