当前位置:网站首页>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;
}
}
}
边栏推荐
- 帶外和帶內的區別
- 98. Verify the binary search tree ●●
- 2022.6.20-6.26 AI industry weekly (issue 103): new little life
- Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改
- Judge whether the binary tree is a complete binary tree
- grafana工具界面显示报错influxDB Error
- Go language implementation principle -- lock implementation principle
- UVA – 11637 Garbage Remembering Exam (组合+可能性)
- Fiddler Everywhere 3.2.1 Crack
- Summary of binary tree recursive routines
猜你喜欢
Neural structured learning - Part 2: training with natural graphs
Pyqt control part (I)
3:第一章:认识JVM规范2:JVM规范,简介;
Debian 10 installation configuration
动态规划 之 打家劫舍
保研笔记一 软件工程与计算卷二(1-7章)
MySQL replace primary key delete primary key add primary key
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Attacking technology Er - Automation
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
随机推荐
有什么不起眼却挣钱的副业?
Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes
Solution to the packaging problem of asyncsocket long connecting rod
The interface of grafana tool displays an error, incluxdb error
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
2022.6.20-6.26 AI行业周刊(第103期):新的小生命
Code farmers to improve productivity
asp. Net pop-up layer instance
无刷驱动设计——浅谈MOS驱动电路
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
yate. conf
In C#, why can't I modify the member of a value type instance in a foreach loop?
Spire.PDF for NET 8.7.2
Scala concurrent programming (II) akka
Spire Office 7.5.4 for NET
How to improve eloquence
TS type declaration
asp.net弹出层实例
开源crm客户关系统管理系统源码,免费分享
Do you regret becoming a programmer?