C#目录类,文件流相关类(2)
今日份主要内容:目录类,文件流相关类
-
目录类:Directory类,DirectoryInfo类
-
文件的输入与输出类:FileStream类,MemoryStream类,StreamReader类,StreamWriter类,StringReader类,StringWriter类
目录类
在 C# 中,Directory 类和 DirectoryInfo 类都用于处理目录操作。
Directory 类提供了一系列的静态方法,用于对目录进行操作,例如创建、删除、移动目录,获取目录下的文件和子目录等。
DirectoryInfo 类则表示一个目录的实例对象。通过创建 DirectoryInfo 对象,可以对特定的目录进行更细致和灵活的操作。它具有一些属性和方法来获取目录的详细信息,并执行一些目录操作。
窗体的设计
窗体设置
private string selectedPath = string.Empty;
//想调用其他方法里的变量,可以在全局定义一个私有属性,然后在方法里存下来那个变量,在其它方法里就可以用了。
public Form1()
{
InitializeComponent();
listView1.Columns.Add("名称", 250, HorizontalAlignment.Left);
listView1.Columns.Add("修改日期", 300, HorizontalAlignment.Left);
listView1.View = View.Details;//按照详细信息的设置显示出来
}
打开文件夹
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult dialogResult = dialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
selectedPath = dialog.SelectedPath; // 把选择的目录记录下来
LoadFolder(dialog.SelectedPath);
}
}
加载文件夹的函数
private void LoadFolder(string selectedPath)
{
listView1.Items.Clear();
DirectoryInfo directoryInfo = new DirectoryInfo(selectedPath);
DirectoryInfo[] folders = directoryInfo.GetDirectories();
foreach (var folder in folders)
{
if ((folder.Attributes & FileAttributes.Hidden) > 0) continue;
ListViewItem listViewItem = new ListViewItem(folder.Name);
listViewItem.SubItems.Add(folder.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"));
listView1.Items.Add(listViewItem);
}
}
创建文件夹
private void button2_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件夹,再创建子文件夹");
return;
}
if (listView1.SelectedItems.Count > 1)
{
MessageBox.Show("只能选择一个文件夹");
return;
}
// 拿listView1控件选中的某项,目的拿到文件夹的名称:selectedItem.Text
ListViewItem selectedItem = listView1.SelectedItems[0];
// 合并一个完整的路径,包括:盘符,选中的文件夹,新文件夹的名称
string fullPath = Path.Combine(selectedPath, selectedItem.Text, textBox1.Text);
DirectoryInfo directoryInfo = new DirectoryInfo(fullPath);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
string parentPath = Path.Combine(selectedPath, selectedItem.Text);
LoadFolder(parentPath);
}
创建子文件夹
private void button3_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件夹,再创建子文件夹");
return;
}
if (listView1.SelectedItems.Count > 1)
{
MessageBox.Show("只能选择一个文件夹");
return;
}
ListViewItem selectedItem = listView1.SelectedItems[0];
string folderPath = Path.Combine(selectedPath, selectedItem.Text);
DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
directoryInfo.CreateSubdirectory(textBox1.Text);
LoadFolder(folderPath);
}
移动文件夹
private void button4_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件夹,再创建子文件夹");
return;
}
if (listView1.SelectedItems.Count > 1)
{
MessageBox.Show("只能选择一个文件夹");
return;
}
ListViewItem selectedItem = listView1.SelectedItems[0];
string sourceDirName = Path.Combine(selectedPath, selectedItem.Text);
DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirName);
string parentPath = directoryInfo.Parent.Parent.FullName;
string destDirName = Path.Combine(parentPath, selectedItem.Text);
Directory.Move(sourceDirName, destDirName);
LoadFolder(parentPath);
}
移除文件夹
private void button5_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件夹,再创建子文件夹");
return;
}
if (listView1.SelectedItems.Count > 1)
{
MessageBox.Show("只能选择一个文件夹");
return;
}
ListViewItem selectedItem = listView1.SelectedItems[0];
string sourceDirName = Path.Combine(selectedPath, selectedItem.Text);
DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirName);
directoryInfo.Delete(); // 参数true时,可以删除子文件夹及文件
LoadFolder(selectedPath);
}
文件流相关类
C#语言中主要包括三种流:文件流FileStream,内存流MemoryStream,网络流NetworkStream。流:语言中数据的一种形式。
普通文件流FileStream
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog.FileName;
using (FileStream fs = new FileStream(path, FileMode.Open))
{
// fs.Length流的长度(流中数据的字节数)
byte[] array = new byte[fs.Length];
fs.Read(array, 0, array.Length);
//Encoding.GetEncoding("UTF-8")
richTextBox1.Text = Encoding.UTF8.GetString(array);
fs.Close();// 可省略
}
}
}
异步读取FileStream
// 方法前添加async关键字,称为异步方法
// await关键字必须配合async关键字来使用。
// 多线程:Thread, ThreadPool, Task, Task<T>
// 异步:BeginXXX配合EndXXX, async结尾,async/await
private async void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog.FileName;
byte[] array;
using (FileStream fs = new FileStream(path, FileMode.Open))
{
array = new byte[fs.Length];
await fs.ReadAsync(array, 0, array.Length);
}
richTextBox1.Text = Encoding.UTF8.GetString(array);
}
}
内存流MemoryStrem
private void button3_Click(object sender, EventArgs e)
{
// 定义一个字符串
string str = "hello world中国";
// 把字符串转换成字节数组
byte[] buffer = Encoding.UTF8.GetBytes(str);
// 定义一个新的字节数组,用来从流中读取数据,存储到新的字节数组中
byte[] readBuffer = new byte[buffer.Length];
// 实例化流时,把字节数组通过流的构造函数放到流中。
using (MemoryStream ms = new MemoryStream(buffer))
{
ms.Read(readBuffer, 0, readBuffer.Length);
string readStr = Encoding.UTF8.GetString(readBuffer);
richTextBox1.Text = readStr;
}
}
MemoryStream(写)
MemoryStream ms = null;
private void button4_Click(object sender, EventArgs e)
{
// buffer缓冲区
/*byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
ms = new MemoryStream(buffer); // 实例化,带数据*/
byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
ms = new MemoryStream(); // 不带数据
ms.Write(buffer, 0, buffer.Length);// 把buffer字节数组中数据写入流
// 在读取或写入数据后,需要重置MemoryStream,以便重新使用,可以使用Seek方法将MemoryStream的位置重置为起始位置
ms.Seek(0, SeekOrigin.Begin);
//ms.Position = 0;
// 切记:此处流不能关闭。
}
MemoryStream(读)
private void button5_Click(object sender, EventArgs e)
{
if (ms == null) return;
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
ms.Close();
richTextBox1.Text = Encoding.UTF8.GetString(buffer);
}
StreamWriter
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog.FileName;
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("HELLO WORLD");
sw.WriteLine("中文");
}
}
}
StreamReader
private void button7_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog.FileName;
// 重命名快捷键:ctrl+r+r
using (StreamReader sr = new StreamReader(path))
{
richTextBox1.Text = sr.ReadToEnd();
}
}
}
StringWriter
private void button9_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("hello world中文1");
sb.AppendLine("hello world中文2");
// StringWriter写入流,StringReader读取流
using (StringWriter sw = new StringWriter(sb))
{
sw.WriteLine("hello world中文3");
}
// 读取
using (StringReader sr = new StringReader(sb.ToString()))
{
string result = sr.ReadToEnd();
richTextBox1.Text = result;
};
}
StringReader
private void button8_Click(object sender, EventArgs e)
{
StringReader sr = new StringReader("fdsfdds");
string result = sr.ReadToEnd();
richTextBox1.Text = result;
}
规律
- 方法上带s结尾的基本上都是返回集合。
- 以is,has,contains结尾的返回bool值。