当前位置:网站首页>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;
}
}
}
边栏推荐
- Analysis of hot spots in AI technology industry
- 2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
- 计算序列之和
- 最详细MySql安装教程
- 洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
- 618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
- 华为游戏初始化init失败,返回错误码907135000
- Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
- Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
- [paid promotion] collection of frequently asked questions, recommended list FAQ
猜你喜欢

Easyexcel, a concise, fast and memory saving excel processing tool
![Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)](/img/c2/bb85b681af0f78b380b1d179c7ea49.png)
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)

二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
![[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]](/img/50/22f2fa8fd606572b13a18cc889ca2e.png)
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]

从.bag文件中读取并保存.jpg图片和.pcd点云

最详细MySql安装教程

How to implement tabbar title bar with list component

How to transfer event objects and user-defined parameters simultaneously in Huawei express applications

Hdu1228 a + B (map mapping)

如何用list组件实现tabbar标题栏
随机推荐
Why does LabVIEW lose precision in floating point numbers
[applinking practical case] share in app pictures through applinking
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
【ARK UI】HarmonyOS ETS的启动页的实现
QT学习日记7——QMainWindow
Static variables in static function
2. Hacking lab script off [detailed writeup]
Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
Hdu1228 a + B (map mapping)
Oracle 笔记
static 函数中的静态变量
二叉树专题--AcWing 1589. 构建二叉搜索树
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
Special topic of binary tree -- acwing 1589 Building binary search tree
[play with FPGA learning 5 in simple terms ----- reset design]
TIPC Getting Started6
华为联机对战服务玩家掉线重连案例总结
【AI应用】海康威视iVMS-4200软件安装