当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
JVM之垃圾回收器
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
flink二开,实现了个 batch lookup join(附源码)
快应用中实现自定义抽屉组件
Matlab processing of distance measurement of experimental electron microscope
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
[AI application] Hikvision ivms-4200 software installation
【AI应用】海康威视iVMS-4200软件安装
[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
Huawei game failed to initialize init with error code 907135000
随机推荐
一招快速实现自定义快应用titlebar
Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
[quick application] win7 system cannot run and debug projects using Huawei ide
JSP webshell free -- webshell free
[play with FPGA learning 5 in simple terms ----- reset design]
PCL extracts a subset from a point cloud
Tick Data and Resampling
Implementation of six singleton modes
Approximate sum count (approximate
Multi line display and single line display of tqdm
V2X-Sim数据集(上海交大&纽约大学)
QT学习日记8——资源文件添加
TIPC introduction 1
tidb-dm报警DM_sync_process_exists_with_error排查
金山云——2023届暑期实习
How does the whole network display IP ownership?
Win11 arm system configuration Net core environment variable
Importerror: impossible d'importer le nom « graph» de « graphviz»
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)