C#文件和文件夹的基本操作

文件流,文件夹的基本操作。有一些异常的判断没有加,实际中可以加一些异常的判断。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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(""); // 这个只能删除为空的目录
}
}
}

效果:

文件操作

感谢支持!