分享一个我常用的c# winform项目 文件和文件夹操作类源代码,用起来比较方便,每个项目几乎都要用到

C#Winform项目中的文件和文件夹操作实用类代码

这是一个我比较常用的c# winform项目文件和文件夹操作类代码,因为几乎每个项目都需要用到文件和文件夹的操作,所以日积月累就写了这么多比较常用的函数,写的不对的地方,还请高人点评。

如果下面代码对你有用,请点个赞。

using System;
using System.IO;

public class FileFolderHandler
{
    // 文件处理

    // 文件读取
    public string ReadFile(string filePath)
    {
        if (File.Exists(filePath))
        {
            return File.ReadAllText(filePath);
        }
        else
        {
            throw new FileNotFoundException("文件不存在!");
        }
    }

    // 文件复制
    public void CopyFile(string sourcePath, string destinationPath)
    {
        File.Copy(sourcePath, destinationPath, true);
    }

    // 文件改名
    public void RenameFile(string filePath, string newFileName)
    {
        if (File.Exists(filePath))
        {
            string directory = Path.GetDirectoryName(filePath);
            string newFilePath = Path.Combine(directory, newFileName);
            File.Move(filePath, newFilePath);
        }
        else
        {
            throw new FileNotFoundException("文件不存在!");
        }
    }

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

    // 判断文件是否存在
    public bool IsFileExists(string filePath)
    {
        return File.Exists(filePath);
    }

    // 文件格式判断
    public string GetFileExtension(string filePath)
    {
        return Path.GetExtension(filePath);
    }

    // 文件大小读取
    public long GetFileSize(string filePath)
    {
        if (File.Exists(filePath))
        {
            return new FileInfo(filePath).Length;
        }
        else
        {
            throw new FileNotFoundException("文件不存在!");
        }
    }

    // 文件夹处理

    // 遍历文件夹所有文件
    public string[] GetAllFilesInFolder(string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            return Directory.GetFiles(folderPath);
        }
        else
        {
            throw new DirectoryNotFoundException("文件夹不存在!");
        }
    }

    // 遍历文件夹所有文件包含子文件夹
    public string[] GetAllFilesInFolderRecursive(string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            return Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
        }
        else
        {
            throw new DirectoryNotFoundException("文件夹不存在!");
        }
    }

    // 判断文件夹是否存在
    public bool IsFolderExists(string folderPath)
    {
        return Directory.Exists(folderPath);
    }

    // 创建文件夹
    public void CreateFolder(string folderPath)
    {
        Directory.CreateDirectory(folderPath);
    }

    // 删除文件夹
    public void DeleteFolder(string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            Directory.Delete(folderPath, true);
        }
        else
        {
            throw new DirectoryNotFoundException("文件夹不存在!");
        }
    }

    // 文件夹文件大小统计
    public long GetFolderSize(string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            long size = 0;
            string[] files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                size += new FileInfo(file).Length;
            }
            return size;
        }
        else
        {
            throw new DirectoryNotFoundException("文件夹不存在!");
        }
    }

    // 文件夹隐藏属性修改
    public void SetFolderHiddenAttribute(string folderPath, bool isHidden)
    {
        if (Directory.Exists(folderPath))
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
            directoryInfo.Attributes = isHidden ? directoryInfo.Attributes | FileAttributes.Hidden : directoryInfo.Attributes & ~FileAttributes.Hidden;
        }
        else
        {
            throw new DirectoryNotFoundException("文件夹不存在!");
        }
    }

    // 文件夹改名
    public void RenameFolder(string folderPath, string newFolderName)
    {
        if (Directory.Exists(folderPath))
        {
            string directory = Path.GetDirectoryName(folderPath);
            string newFolderPath = Path.Combine(directory, newFolderName);
            Directory.Move(folderPath, newFolderPath);
        }
        else
        {
            throw new DirectoryNotFoundException("文件夹不存在!");
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值