C#----文件操作(文件管理,目录管理,文件读写)

1 篇文章 0 订阅

文件管理(File类和FileInfo类)

首先,再使用File类和FileInfo类前要先引用IO
using System.IO;

File类(文件操作类)

方法

  1. Copy(String sourceFileName, String destFileName) 将现有文件复制到新文件。 不允许覆盖同名的文件
    Copy(String, String, Boolean) 将现有文件复制到新文件。 允许覆盖同名的文件。
  2. Create(String path) 在指定路径中创建或覆盖文件。
  3. Delete (string path); 删除指定的文件
  4. bool Exists (string path); 确定指定的文件是否存在
  5. Move(String sourceFileName, String destFileName) 将指定文件移到新位置,提供要指定新文件名的选项。
  6. FileStream Open (string path, System.IO.FileMode mode) 打开指定路径上的 FileStream。 返回一个FileStream对象

FileInfo类

提供用于创建、复制、删除、移动和打开文件的属性和实例方法,并且帮助创建 FileStream 对象。

**构造函数:**public FileInfo (string fileName);

方法:

  1. CopyTo(String) 将现有文件复制到新文件,不允许覆盖现有文件。
    CopyTo(String, Boolean) 将现有文件复制到新文件,允许覆盖现有文件。
  2. FileStream Create (); 创建文件。
  3. void Delete (); 永久删除文件。
  4. MoveTo(String) 将指定文件移到新位置,提供要指定新文件名的选项。
  5. Open(FileMode) 在指定的模式中打开文件。
  6. string ToString (); 以字符串形式返回路径。 将 Name 属性用于完整路径。
    属性
  7. Directory 获取父目录的实例。
  8. DirectoryName 获取表示目录的完整路径的字符串。
  9. Exists 获取指示文件是否存在的值。
  10. Name 获取文件名。
  11. 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类
方法

  1. CreateDirectory(String) 在指定路径中创建所有目录和子目录,除非它们已经存在。
  2. Delete(String) 从指定路径删除空目录。
  3. GetFiles(String) 返回指定目录中文件的名称(包括其路径)。
  4. GetDirectories(String) 返回指定目录中的子目录的名称(包括其路径)。
  5. GetDirectoryRoot (string path); 返回指定路径的卷信息、根信息或两者同时返回
  6. Move (string sourceDirName, string destDirName); 将文件或目录及其内容移到新位置。
  7. GetFileSystemEntries(String) 返回指定路径中的所有文件和子目录的名称。

DirectoryInfo类
初始化 DirectoryInfo (string path);

方法

  1. Create() 创建目录。
    Create(DirectorySecurity) 使用 DirectorySecurity 对象创建目录。
  2. CreateSubdirectory(String) 在指定路径上创建一个或多个子目录。 指定路径可以是相对于 DirectoryInfo 类的此实例的路径。
  3. GetDirectories() 返回当前目录的子目录。–一般使用foreach循环来遍历子目录
  4. GetFiles() 返回当前目录的文件列表。
    GetFiles(String, SearchOption) 返回当前目录的文件列表,该列表与给定的搜索模式匹配并且使用某个值确定是否搜索子目录。
  5. 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的方法

  1. int Read(byte[] array,int offset, int count) 从流中读取子块,并将改数据写入给定的缓冲区中
  2. byte ReadByte() 从文件中读取一个字节,并将读取的位置加一个字节
  3. Write(byte[] array, int offset, int count) 使用从缓冲区读取的数据将字节块写入该流
  4. 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();
                }

            }
        }
    }
}

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值