当前位置:网站首页>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;
}
}
}
边栏推荐
- Rasa 3. X learning series -rasa 3.2.1 new release
- 做自媒体影视短视频剪辑号,在哪儿下载素材?
- 开关电源Buck电路CCM及DCM工作模式
- Cwaitabletimer timer, used to create timer object access
- Neural structured learning 4 antagonistic learning for image classification
- Code farmers to improve productivity
- 2022.6.20-6.26 AI行业周刊(第103期):新的小生命
- 判斷二叉樹是否為完全二叉樹
- (4) UART application design and simulation verification 2 - RX module design (stateless machine)
- QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
猜你喜欢

orgchart. JS organization chart, presenting structural data in an elegant way
![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)
[original] what is the core of programmer team management?

Huawei simulator ENSP - hcip - MPLS experiment

Basic knowledge of database (interview)

21.PWM应用编程

动态规划 之 打家劫舍

2:第一章:认识JVM规范1:JVM简介;

Sum of two numbers, sum of three numbers (sort + double pointer)

Dynamic planning: robbing families and houses

TVS管和ESD管的技術指標和選型指南-嘉立創推薦
随机推荐
开关电源Buck电路CCM及DCM工作模式
Comparison of parameters between TVs tube and zener diode
Summary of binary tree recursive routines
424. The longest repeated character after replacement ●●
The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous
Fiddler Everywhere 3.2.1 Crack
White hat talks about web security after reading 2
Sum of two numbers, sum of three numbers (sort + double pointer)
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
进击的技术er——自动化
Pyqt control part (I)
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
UVA – 11637 garbage remembering exam (combination + possibility)
regular expression
Go language introduction detailed tutorial (I): go language in the era
Go language implementation principle -- lock implementation principle
424. 替换后的最长重复字符 ●●
Dynamic memory management (malloc/calloc/realloc)
3D reconstruction of point cloud
《牛客刷verilog》Part III Verilog企业真题