当前位置:网站首页>C file and folder operation
C file and folder operation
2022-07-02 11:14:00 【Tingyi -- flying bird】
File path ==> Get the file name
Combine File path
// Get the file name
string name = System.IO.Path.GetFileName(file);
// Combined file path
string dest = System.IO.Path.Combine(destFolder, name);
Does the folder exist
Directory.Exists(folderPath);
Does the file exist
File.Exists(fileFullName);
File and folder operations
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
{
// obtain Folder list
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;
}
// obtain File list
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;
}
// Copy folder
public static bool CopyFolder(string sourceFolder, string destFolder)
{
try
{
// If the target path does not exist , Then create the target path
if (!System.IO.Directory.Exists(destFolder))
{
System.IO.Directory.CreateDirectory(destFolder);
}
// Get all the files in the root directory of the original file
string[] files = System.IO.Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
// Get the file name
string name = System.IO.Path.GetFileName(file);
// Combined file path
string dest = System.IO.Path.Combine(destFolder, name);
System.IO.File.Copy(file, dest);// Copy file
}
// Get all folders under the root directory of the original file
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);// Build the target path , Recursively copy files
}
return true;
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
}
// Delete folder
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;
}
}
}
边栏推荐
- Importerror: impossible d'importer le nom « graph» de « graphviz»
- static 函数中的静态变量
- 二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
- 最详细MySql安装教程
- 启牛商学院给的股票账户安全吗?能开户吗?
- flink二開,實現了個 batch lookup join(附源碼)
- 【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
- 【ARK UI】HarmonyOS ETS的启动页的实现
- PCL extracts a subset from a point cloud
- QT learning diary 7 - qmainwindow
猜你喜欢

ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘

Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)

【AI应用】海康威视iVMS-4200软件安装
![[in simple terms, play with FPGA learning 3 ----- basic grammar]](/img/f0/0204fa5197033877dc0758203253ae.png)
[in simple terms, play with FPGA learning 3 ----- basic grammar]

从攻击面视角,看信创零信任方案实践

一招快速实现自定义快应用titlebar
![[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)]

如何用list组件实现tabbar标题栏

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

快应用中实现自定义抽屉组件
随机推荐
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
[in simple terms, play with FPGA learning 3 ----- basic grammar]
金山云——2023届暑期实习
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
Use of vscode tool
Array splitting (regular thinking
K-d tree and octree of PCL
力扣(LeetCode)182. 查找重复的电子邮箱(2022.07.01)
Implement custom drawer component in quick application
Win11 arm system configuration Net core environment variable
sql left join 主表限制条件写在on后面和写在where后面的区别
【IDEA】使用插件一键逆向生成代码
php中self和static在方法中的区别
【深入浅出玩转FPGA学习3-----基本语法】
enumrate的start属性的坑
One trick to quickly realize custom application titlebar
Openmldb meetup No.4 meeting minutes
ros gazebo相关包的安装
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
TIPC Getting Started6