当前位置:网站首页>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;
}
}
}
边栏推荐
- Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
- 函数式接口和方法引用
- Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
- static 函数中的静态变量
- TIPC Service and Topology Tracking4
- [quick application] win7 system cannot run and debug projects using Huawei ide
- Overview of integrated learning
- ctf 记录
- 二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
- The difference between self and static in PHP in methods
猜你喜欢

ctf 记录

V2X-Sim数据集(上海交大&纽约大学)

V2x SIM dataset (Shanghai Jiaotong University & New York University)

二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)

二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)

How does the whole network display IP ownership?

TIPC Cluster5

PHP tea sales and shopping online store

III Chip startup and clock system

JSP webshell free -- webshell free
随机推荐
【深入浅出玩转FPGA学习4----漫谈状态机设计】
K-d tree and octree of PCL
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
快应用中实现自定义抽屉组件
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
String (Analog
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
Thanos Receiver
TIPC协议
How to implement tabbar title bar with list component
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
Array splitting (regular thinking
What are the software product management systems? Inventory of 12 best product management tools
PCL point cloud to depth image
Use Huawei performance management service to configure the sampling rate on demand
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
Indexer in C #
[applinking practical case] share in app pictures through applinking
TIPC Cluster5
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)