当前位置:网站首页>C# 文件与文件夹操作
C# 文件与文件夹操作
2022-07-05 23:26:00 【廷益--飞鸟】
文件路径 ==>获取文件名
组合 文件路径
// 获取文件名
string name = System.IO.Path.GetFileName(file);
// 组合文件路径
string dest = System.IO.Path.Combine(destFolder, name);
文件夹是否存在
Directory.Exists(folderPath);
文件是否存在
File.Exists(fileFullName);
文件与文件夹操作
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SettingROI
{
class UtilityFile
{
// 获取 文件夹列表
public static List<string> GetAllDir(string baseDir)
{
DirectoryInfo srcDirInfo = new DirectoryInfo(baseDir);
DirectoryInfo[] srcDirs = srcDirInfo.GetDirectories();
List<string> strSrcDirLst = new List<string>();
foreach (DirectoryInfo item in srcDirs)
{
string fileName = item.FullName;
strSrcDirLst.Add(fileName);
}
return strSrcDirLst;
}
// 获取 文件列表
public static List<string> GetAllFileNames(string path, string pattern = "*")
{
DirectoryInfo folder = new DirectoryInfo(path);
List<string> fileNames = new List<string>();
foreach (FileInfo file in folder.GetFiles(pattern))
{
fileNames.Add(file.Name);
}
return fileNames;
}
// 拷贝文件夹
public static bool CopyFolder(string sourceFolder, string destFolder)
{
try
{
//如果目标路径不存在,则创建目标路径
if (!System.IO.Directory.Exists(destFolder))
{
System.IO.Directory.CreateDirectory(destFolder);
}
//得到原文件根目录下的所有文件
string[] files = System.IO.Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
// 获取文件名
string name = System.IO.Path.GetFileName(file);
// 组合文件路径
string dest = System.IO.Path.Combine(destFolder, name);
System.IO.File.Copy(file, dest);//复制文件
}
//得到原文件根目录下的所有文件夹
string[] folders = System.IO.Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = System.IO.Path.GetFileName(folder);
string dest = System.IO.Path.Combine(destFolder, name);
CopyFolder(folder, dest);//构建目标路径,递归复制文件
}
return true;
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
}
// 删除文件夹
public static bool DeleteFolder(string deleteFolder)
{
try
{
if (Directory.Exists(deleteFolder))
Directory.Delete(deleteFolder, true);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
}
}
边栏推荐
猜你喜欢
Xinyuan & Lichuang EDA training camp - brushless motor drive
Brushless drive design -- on MOS drive circuit
YML configuration, binding and injection, verification, unit of bean
Comparison of parameters between TVs tube and zener diode
进击的技术er——自动化
Go language implementation principle -- map implementation principle
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Data analysis - Thinking foreshadowing
随机推荐
2:第一章:认识JVM规范1:JVM简介;
【经典控制理论】自控实验总结
How to design API return code (error code)?
Summary of binary tree recursive routines
Dynamic planning: robbing families and houses
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
The interface of grafana tool displays an error, incluxdb error
3:第一章:认识JVM规范2:JVM规范,简介;
Krypton Factor purple book chapter 7 violent solution
Common static methods of math class
Comparison of parameters between TVs tube and zener diode
grafana工具界面显示报错influxDB Error
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
698. Divided into k equal subsets ●●
CIS benchmark tool Kube bench
MySQL (2) -- simple query, conditional query
Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
3D reconstruction of point cloud
Judge whether the binary tree is a complete binary tree
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)