当前位置:网站首页>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;
}
}
}
边栏推荐
- [applinking practical case] share in app pictures through applinking
- Hdu1234 door opener and door closer (water question)
- 【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
- Read H264 parameters from mediarecord recording
- Convert yv12 to rgb565 image conversion, with YUV to RGB test
- HDU1236 排名(结构体排序)
- [play with FPGA learning 5 in simple terms ----- reset design]
- The first white paper on agile practice in Chinese enterprises was released | complete download is attached
- Why does LabVIEW lose precision in floating point numbers
- 全网显示 IP 归属地,是怎么实现的?
猜你喜欢
Hdu1234 door opener and door closer (water question)
【AGC】构建服务3-认证服务示例
如何使用IDE自动签名调试鸿蒙应用
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
V2X-Sim数据集(上海交大&纽约大学)
2. Hacking lab script off [detailed writeup]
QT学习日记8——资源文件添加
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
[quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
随机推荐
Hdu1228 a + B (map mapping)
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
TIPC介绍1
首份中国企业敏捷实践白皮书发布| 附完整下载
Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
华为联机对战服务玩家掉线重连案例总结
2. Hacking lab script off [detailed writeup]
OpenMLDB Meetup No.4 会议纪要
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
【AI应用】海康威视iVMS-4200软件安装
Oracle 笔记
华为应用市场应用统计数据问题大揭秘
[quick application] win7 system cannot run and debug projects using Huawei ide
全网显示 IP 归属地,是怎么实现的?
Appgallery connect scenario development practice - image storage and sharing
Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
正则及常用公式
AppGallery Connect场景化开发实战—图片存储分享
Indexer in C #