C#理论-提供一个常用的.NET 8 文件和文件夹操作管理类和详解,每行都有中文注解,你一定能看得懂,希望对新手有所帮助

引言

文件和文件夹操作是软件开发中常见的任务。本文将介绍如何使用 C# .NET 8 实现一个完整的文件和文件夹操作管理类,包括文件的读写、改名、移动、复制、修改属性、文件夹读取、遍历、获取文件夹大小以及文件增删改等操作。所有代码行都附有中文注释,适合新手学习使用。

完整代码示例

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace FileManager
{
    public class FileOperations
    {
        // 读取文件内容
        public string ReadFile(string filePath)
        {
            // 检查文件是否存在
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("文件不存在");
            }
            // 读取文件内容并返回
            return File.ReadAllText(filePath);
        }

        // 写入内容到文件
        public void WriteFile(string filePath, string content)
        {
            // 写入内容到文件(如果文件不存在则创建)
            File.WriteAllText(filePath, content);
        }

        // 重命名文件
        public void RenameFile(string filePath, string newFileName)
        {
            // 获取文件目录
            string directory = Path.GetDirectoryName(filePath);
            // 构建新的文件路径
            string newFilePath = Path.Combine(directory, newFileName);

            // 检查文件是否存在
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("文件不存在");
            }

            // 检查新文件名是否已存在
            if (File.Exists(newFilePath))
            {
                throw new IOException("新文件名已存在");
            }

            // 重命名文件
            File.Move(filePath, newFilePath);
        }

        // 移动文件
        public void MoveFile(string sourceFilePath, string destinationFilePath)
        {
            // 检查源文件是否存在
            if (!File.Exists(sourceFilePath))
            {
                throw new FileNotFoundException("源文件不存在");
            }

            // 检查目标文件是否已存在
            if (File.Exists(destinationFilePath))
            {
                throw new IOException("目标文件已存在");
            }

            // 移动文件
            File.Move(sourceFilePath, destinationFilePath);
        }

        // 复制文件
        public void CopyFile(string sourceFilePath, string destinationFilePath)
        {
            // 检查源文件是否存在
            if (!File.Exists(sourceFilePath))
            {
                throw new FileNotFoundException("源文件不存在");
            }

            // 检查目标文件是否已存在
            if (File.Exists(destinationFilePath))
            {
                throw new IOException("目标文件已存在");
            }

            // 复制文件
            File.Copy(sourceFilePath, destinationFilePath);
        }

        // 修改文件属性
        public void ModifyFileAttributes(string filePath, FileAttributes attributes)
        {
            // 检查文件是否存在
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("文件不存在");
            }

            // 修改文件属性
            File.SetAttributes(filePath, attributes);
        }

        // 读取文件夹内容
        public List<string> ReadDirectory(string directoryPath)
        {
            // 检查文件夹是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException("文件夹不存在");
            }

            // 获取文件夹中的所有文件和子文件夹
            var entries = Directory.GetFileSystemEntries(directoryPath).ToList();
            return entries;
        }

        // 遍历文件夹
        public List<string> TraverseDirectory(string directoryPath)
        {
            // 检查文件夹是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException("文件夹不存在");
            }

            // 获取文件夹中的所有文件和子文件夹,包括子文件夹中的内容
            var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories).ToList();
            return files;
        }

        // 获取文件夹大小
        public long GetDirectorySize(string directoryPath)
        {
            // 检查文件夹是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException("文件夹不存在");
            }

            // 获取文件夹中所有文件的大小
            var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
            long size = files.Sum(file => new FileInfo(file).Length);
            return size;
        }

        // 删除文件
        public void DeleteFile(string filePath)
        {
            // 检查文件是否存在
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("文件不存在");
            }

            // 删除文件
            File.Delete(filePath);
        }

        // 创建文件夹
        public void CreateDirectory(string directoryPath)
        {
            // 检查文件夹是否存在
            if (Directory.Exists(directoryPath))
            {
                throw new IOException("文件夹已存在");
            }

            // 创建文件夹
            Directory.CreateDirectory(directoryPath);
        }

        // 删除文件夹
        public void DeleteDirectory(string directoryPath, bool recursive = false)
        {
            // 检查文件夹是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException("文件夹不存在");
            }

            // 删除文件夹
            Directory.Delete(directoryPath, recursive);
        }

        // 重命名文件夹
        public void RenameDirectory(string directoryPath, string newDirectoryName)
        {
            // 获取父文件夹路径
            string parentDirectory = Directory.GetParent(directoryPath).FullName;
            // 构建新的文件夹路径
            string newDirectoryPath = Path.Combine(parentDirectory, newDirectoryName);

            // 检查文件夹是否存在
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException("文件夹不存在");
            }

            // 检查新文件夹名是否已存在
            if (Directory.Exists(newDirectoryPath))
            {
                throw new IOException("新文件夹名已存在");
            }

            // 重命名文件夹
            Directory.Move(directoryPath, newDirectoryPath);
        }

        // 移动文件夹
        public void MoveDirectory(string sourceDirectoryPath, string destinationDirectoryPath)
        {
            // 检查源文件夹是否存在
            if (!Directory.Exists(sourceDirectoryPath))
            {
                throw new DirectoryNotFoundException("源文件夹不存在");
            }

            // 检查目标文件夹是否已存在
            if (Directory.Exists(destinationDirectoryPath))
            {
                throw new IOException("目标文件夹已存在");
            }

            // 移动文件夹
            Directory.Move(sourceDirectoryPath, destinationDirectoryPath);
        }

        // 复制文件夹
        public void CopyDirectory(string sourceDirectoryPath, string destinationDirectoryPath)
        {
            // 检查源文件夹是否存在
            if (!Directory.Exists(sourceDirectoryPath))
            {
                throw new DirectoryNotFoundException("源文件夹不存在");
            }

            // 检查目标文件夹是否已存在
            if (Directory.Exists(destinationDirectoryPath))
            {
                throw new IOException("目标文件夹已存在");
            }

            // 创建目标文件夹
            Directory.CreateDirectory(destinationDirectoryPath);

            // 获取源文件夹中的所有文件
            foreach (var file in Directory.GetFiles(sourceDirectoryPath, "*", SearchOption.AllDirectories))
            {
                string destinationFile = file.Replace(sourceDirectoryPath, destinationDirectoryPath);
                string destinationDir = Path.GetDirectoryName(destinationFile);

                // 创建目标文件夹
                if (!Directory.Exists(destinationDir))
                {
                    Directory.CreateDirectory(destinationDir);
                }

                // 复制文件
                File.Copy(file, destinationFile);
            }
        }
    }
}

使用示例

下面是如何使用 FileOperations 类的示例代码:

using System;

namespace FileManager
{
    class Program
    {
        static void Main(string[] args)
        {
            FileOperations fileOps = new FileOperations();

            // 示例文件路径和内容
            string filePath = "example.txt";
            string content = "Hello, World!";
            
            // 写入文件
            fileOps.WriteFile(filePath, content);
            Console.WriteLine("文件写入完成");

            // 读取文件
            string readContent = fileOps.ReadFile(filePath);
            Console.WriteLine("读取内容: " + readContent);

            // 重命名文件
            string newFileName = "renamed_example.txt";
            fileOps.RenameFile(filePath, newFileName);
            Console.WriteLine("文件重命名完成");

            // 移动文件
            string destinationFilePath = "moved_example.txt";
            fileOps.MoveFile(newFileName, destinationFilePath);
            Console.WriteLine("文件移动完成");

            // 复制文件
            string copiedFilePath = "copied_example.txt";
            fileOps.CopyFile(destinationFilePath, copiedFilePath);
            Console.WriteLine("文件复制完成");

            // 删除文件
            fileOps.DeleteFile(destinationFilePath);
            Console.WriteLine("文件删除完成");

            // 创建文件夹
            string directoryPath = "example_dir";
            fileOps.CreateDirectory(directoryPath);
            Console.WriteLine("文件夹创建完成");

            // 读取文件夹内容
            var entries = fileOps.ReadDirectory(directoryPath);
            Console.WriteLine("文件夹内容: " + string.Join(", ", entries));

            // 获取文件夹大小
            long size = fileOps.GetDirectorySize(directoryPath);
            Console.WriteLine("文件夹大小: " + size + " bytes");

            // 删除文件夹
            fileOps.DeleteDirectory(directoryPath);
            Console.WriteLine("文件夹删除完成");
        }
    }
}

结论

通过本文,你已经学会了如何使用 C# .NET 8 实现一个完整的文件和文件夹操作管理类。这个类包含了文件的读写、改名、移动、复制、修改属性、文件夹读取、遍历、获取文件夹大小以及文件增删改等操作。你可以根据实际需求进一步扩展和优化代码,添加更多功能和错误处理机制,以提升应用程序的稳定性和用户体验。希望这篇文章能为你的开发工作提供帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值