当前位置:网站首页>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;
}
}
}
边栏推荐
- (4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
- Comparison between webgl and webgpu [3] - vertex buffer
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- 《牛客刷verilog》Part III Verilog企业真题
- Non rigid / flexible point cloud ICP registration
- Go语言实现原理——锁实现原理
- VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
- Dynamic memory management (malloc/calloc/realloc)
- 698. Divided into k equal subsets ●●
- (4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
猜你喜欢

Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang

开关电源Buck电路CCM及DCM工作模式

Data analysis - Thinking foreshadowing

Go language introduction detailed tutorial (I): go language in the era

698. 划分为k个相等的子集 ●●

芯源&立创EDA训练营——无刷电机驱动

STM32__ 06 - single channel ADC

进击的技术er——自动化

基于脉冲神经网络的物体检测

无刷驱动设计——浅谈MOS驱动电路
随机推荐
UVA11294-Wedding(2-SAT)
秒杀系统的设计与实现思路
俄外交部:日韩参加北约峰会影响亚洲安全稳定
进击的技术er——自动化
Fiddler Everywhere 3.2.1 Crack
Neural structured learning - Part 3: training with synthesized graphs
带外和带内的区别
TVS管 与 稳压二极管参数对比
Basic knowledge of database (interview)
Object detection based on impulse neural network
3:第一章:认识JVM规范2:JVM规范,简介;
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
保研笔记四 软件工程与计算卷二(8-12章)
有什么不起眼却挣钱的副业?
What is the process of building a website
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
Xinyuan & Lichuang EDA training camp - brushless motor drive
The interface of grafana tool displays an error, incluxdb error
Judge whether the binary tree is a complete binary tree
How to insert data into MySQL database- How can I insert data into a MySQL database?