这是一个我比较常用的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("文件夹不存在!");
}
}
}
C#Winform项目中的文件和文件夹操作实用类代码
1991

被折叠的 条评论
为什么被折叠?



