当前位置:网站首页>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;
}
}
}
边栏推荐
- Breadth first search open turntable lock
- 做自媒体影视短视频剪辑号,在哪儿下载素材?
- [classical control theory] summary of automatic control experiment
- Go language implementation principle -- lock implementation principle
- 698. Divided into k equal subsets ●●
- UVA11294-Wedding(2-SAT)
- 有什么不起眼却挣钱的副业?
- Creative mode 1 - single case mode
- 保研笔记二 软件工程与计算卷二(13-16章)
- Brushless drive design -- on MOS drive circuit
猜你喜欢
Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes
21.PWM应用编程
Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
698. Divided into k equal subsets ●●
Use mapper: --- tkmapper
成为程序员的你,后悔了吗?
Do you regret becoming a programmer?
Fiddler Everywhere 3.2.1 Crack
同事悄悄告诉我,飞书通知还能这样玩
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
随机推荐
Difference between out of band and in band
Fiddler Everywhere 3.2.1 Crack
YML configuration, binding and injection, verification, unit of bean
Design and implementation of secsha system
哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
Object detection based on impulse neural network
21. PWM application programming
Idea connects to MySQL, and it is convenient to paste the URL of the configuration file directly
无刷驱动设计——浅谈MOS驱动电路
idea 连接mysql ,直接贴配置文件的url 比较方便
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
动态规划 之 打家劫舍
Huawei simulator ENSP - hcip - MPLS experiment
【LeetCode】5. Valid Palindrome·有效回文
Why use weak pointers for delegation- Why use weak pointer for delegation?
保研笔记一 软件工程与计算卷二(1-7章)
golang代码检查工具
Objective C message dispatch mechanism
asp. Net pop-up layer instance
STM32__06—单通道ADC