当前位置:网站首页>C file and folder operation
C file and folder operation
2022-07-05 23:41: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;
}
}
}
边栏推荐
- 【原创】程序员团队管理的核心是什么?
- 3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
- Solution to the packaging problem of asyncsocket long connecting rod
- 【LeetCode】5. Valid palindrome
- 424. The longest repeated character after replacement ●●
- Opencvsharp (C openCV) shape detection and recognition (with source code)
- 15 MySQL-存储过程与函数
- It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
- [original] what is the core of programmer team management?
- Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
猜你喜欢

orgchart. JS organization chart, presenting structural data in an elegant way

4 points tell you the advantages of the combination of real-time chat and chat robots

Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes

Scala concurrent programming (II) akka

TVS管 与 稳压二极管参数对比

21. PWM application programming

20220703 周赛:知道秘密的人数-动规(题解)

Go language implementation principle -- lock implementation principle

GFS分布式文件系統

CIS benchmark tool Kube bench
随机推荐
GFS分布式文件系统
MySQL delete uniqueness constraint unique
动态规划 之 打家劫舍
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
GFS分布式文件系統
保研笔记一 软件工程与计算卷二(1-7章)
Object detection based on impulse neural network
rsync远程同步
UVA – 11637 Garbage Remembering Exam (组合+可能性)
Development specification: interface unified return value format [resend]
证明 poj 1014 模优化修剪,部分递归 有错误
Golang code checking tool
Scala concurrent programming (II) akka
Spire Office 7.5.4 for NET
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
帶外和帶內的區別
Common static methods of math class
Spire. PDF for NET 8.7.2
Research notes I software engineering and calculation volume II (Chapter 1-7)
Comparison of parameters between TVs tube and zener diode