文件管理(File类和FileInfo类)
首先,再使用File类和FileInfo类前要先引用IO
using System.IO;
File类(文件操作类)
方法
- Copy(String sourceFileName, String destFileName) 将现有文件复制到新文件。 不允许覆盖同名的文件
Copy(String, String, Boolean) 将现有文件复制到新文件。 允许覆盖同名的文件。 - Create(String path) 在指定路径中创建或覆盖文件。
- Delete (string path); 删除指定的文件
- bool Exists (string path); 确定指定的文件是否存在
- Move(String sourceFileName, String destFileName) 将指定文件移到新位置,提供要指定新文件名的选项。
- FileStream Open (string path, System.IO.FileMode mode) 打开指定路径上的 FileStream。 返回一个FileStream对象
FileInfo类
提供用于创建、复制、删除、移动和打开文件的属性和实例方法,并且帮助创建 FileStream 对象。
**构造函数:**public FileInfo (string fileName);
方法:
- CopyTo(String) 将现有文件复制到新文件,不允许覆盖现有文件。
CopyTo(String, Boolean) 将现有文件复制到新文件,允许覆盖现有文件。 - FileStream Create (); 创建文件。
- void Delete (); 永久删除文件。
- MoveTo(String) 将指定文件移到新位置,提供要指定新文件名的选项。
- Open(FileMode) 在指定的模式中打开文件。
- string ToString (); 以字符串形式返回路径。 将 Name 属性用于完整路径。
属性 - Directory 获取父目录的实例。
- DirectoryName 获取表示目录的完整路径的字符串。
- Exists 获取指示文件是否存在的值。
- Name 获取文件名。
- Length 获取当前文件的大小(以字节为单位)。
实例:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
DialogResult result = openFile.ShowDialog();
if (result==DialogResult.OK)
{
FileInfo info = new FileInfo(openFile.FileName);
richTextBox1.Text += "文件名称:" + info.Name + "\n";
richTextBox1.Text += "路径:" + info.FullName + "\n";
}
}
当按下button1时,richtTextBox1中就会显示文件名称和路径
将指定文件移动到指定目录下操作
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;
namespace FileOperation
{
public partial class FileCopyTest : Form
{
string fileName;
public FileCopyTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
DialogResult result = openFileDialog.ShowDialog();
if(result== DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
fileName = System.IO.Path.GetFileName(openFileDialog.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK)
{
textBox2.Text = folderBrowserDialog.SelectedPath;
}
}
private void button3_Click(object sender, EventArgs e)
{
string path = textBox2.Text + "\\" + fileName;
File.Move(textBox1.Text,path);
}
}
}
目录管理
Directory类
方法
- CreateDirectory(String) 在指定路径中创建所有目录和子目录,除非它们已经存在。
- Delete(String) 从指定路径删除空目录。
- GetFiles(String) 返回指定目录中文件的名称(包括其路径)。
- GetDirectories(String) 返回指定目录中的子目录的名称(包括其路径)。
- GetDirectoryRoot (string path); 返回指定路径的卷信息、根信息或两者同时返回
- Move (string sourceDirName, string destDirName); 将文件或目录及其内容移到新位置。
- GetFileSystemEntries(String) 返回指定路径中的所有文件和子目录的名称。
DirectoryInfo类
初始化 DirectoryInfo (string path);
方法
- Create() 创建目录。
Create(DirectorySecurity) 使用 DirectorySecurity 对象创建目录。 - CreateSubdirectory(String) 在指定路径上创建一个或多个子目录。 指定路径可以是相对于 DirectoryInfo 类的此实例的路径。
- GetDirectories() 返回当前目录的子目录。–一般使用foreach循环来遍历子目录
- GetFiles() 返回当前目录的文件列表。
GetFiles(String, SearchOption) 返回当前目录的文件列表,该列表与给定的搜索模式匹配并且使用某个值确定是否搜索子目录。 - MoveTo (string destDirName); 将 DirectoryInfo 实例及其内容移动到新路径。
将文件夹(包含子文件夹)完整拷贝到指定目录
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;
namespace FileOperation
{
public partial class FileCopy : Form
{
public FileCopy()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folder = new FolderBrowserDialog();
DialogResult result = folder.ShowDialog();
if (result == DialogResult.OK)
{
FileInfo info = new FileInfo(folder.SelectedPath);
textBox1.Text = info.FullName;
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog folder = new FolderBrowserDialog();
DialogResult result = folder.ShowDialog();
if (result == DialogResult.OK)
{
FileInfo info = new FileInfo(folder.SelectedPath);
textBox2.Text = info.FullName;
}
}
private void button3_Click(object sender, EventArgs e)
{
DirectoryInfo source = new DirectoryInfo(textBox1.Text);
DirectoryInfo destination = new DirectoryInfo(textBox2.Text);
if (!destination.Exists)
{
destination.Create();
}
foreach(DirectoryInfo directoryInfo in source.GetDirectories())
{
foreach (FileInfo f in directoryInfo.GetFiles())
{
Directory.CreateDirectory(destination.FullName + source.Name+"\\"+directoryInfo.Name);
f.CopyTo(destination.FullName + source.Name + "\\" + directoryInfo.Name + "\\" + f.Name, true); //新建文件夹复制
richTextBox1.Text += f.Name + "已经被复制" + "\n";
}
}
richTextBox1.Text += "复制完成";
}
}
}
结果
文件读写
**流(Stream)**是字节序列的抽象概念,是串行化设备的抽象表示,串行化设备可以用线性方式存储数据,并可以用同样的方式访问设备
流的基本操作:
读取(read) 从流到数据结构的数据传输
写入(write)从数据结构到流的数据传输
查找(seek)对流内当前位置进行查询和修改
在使用FileStream操作文件前,要先创建FIileStream类的实例。
FileStream(string path,FileMode mode)
FileStream(string path,FileMode mode,FileAccess access)
path 文件路径
mode 被操作文件的模式, Append\Create等
access 被操作文件的访问方式 Read, Write,ReadWrite
FileStream的方法
- int Read(byte[] array,int offset, int count) 从流中读取子块,并将改数据写入给定的缓冲区中
- byte ReadByte() 从文件中读取一个字节,并将读取的位置加一个字节
- Write(byte[] array, int offset, int count) 使用从缓冲区读取的数据将字节块写入该流
- WriteByte(byte value) 将一个字节写入流的的当前位置 value 要写入流的字节
使用FileStream来获取文件并存入RichTextBox中
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;
namespace FileOperation
{
public partial class FileRead : Form
{
public FileRead()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
FileStream fs = new FileStream(textBox1.Text,FileMode.Open);
for(int i = 0; i < fs.Length; i++)
{
richTextBox1.Text += (char)fs.ReadByte();
}
}
}
}
}