当前位置:网站首页>C# 文件与文件夹操作
C# 文件与文件夹操作
2022-07-02 07:35: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;
}
}
}
边栏推荐
- 二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
- TIPC介绍1
- Jsp webshell Free from killing - The Foundation of JSP
- 软件产品管理系统有哪些?12个最佳产品管理工具盘点
- Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
- 洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
- Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
- flink二开,实现了个 batch lookup join(附源码)
- Huawei game failed to initialize init with error code 907135000
- 2022-06-17
猜你喜欢
Hdu1234 door opener and door closer (water question)
V2x SIM dataset (Shanghai Jiaotong University & New York University)
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
如何使用IDE自动签名调试鸿蒙应用
2022-06-17
Leetcode+ 76 - 80 storm search topic
One trick to quickly realize custom application titlebar
Nodejs+express+mysql simple blog building
实验电镜距离测量之Matlab处理
QT学习日记8——资源文件添加
随机推荐
JSP webshell free -- the basis of JSP
Implement custom drawer component in quick application
Point cloud projection picture
TIPC Service and Topology Tracking4
【AI应用】海康威视iVMS-4200软件安装
学习open62541 --- [66] UA_String的生成方法
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
如何用list组件实现tabbar标题栏
【深入浅出玩转FPGA学习4----漫谈状态机设计】
How to use ide to automatically sign and debug Hongmeng application
How does the whole network display IP ownership?
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
PCL eigen introduction and simple use
实验电镜距离测量之Matlab处理
TIPC Cluster5
MySQL environment configuration
Convert yv12 to rgb565 image conversion, with YUV to RGB test
What are the software product management systems? Inventory of 12 best product management tools
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
LVM操作