当前位置:网站首页>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;
        }


    }
}

原网站

版权声明
本文为[Tingyi -- flying bird]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207052325208159.html