C# Winform开发 文件/文件夹操作 ControlFileClass类

11 篇文章 1 订阅

参考大佬的代码,自己再根据自己的需求加了一些方法,每一个方法都有对应的传值和返回值的解释。

后续写的一些代码里面使用的ControlFileClass类都是使用的这个类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace origin.Model
{
    /// <summary>
    /// 文件操作类
    /// </summary>
    public class ControlFileClass
    {
        //字段声明
        private ArrayList fileListPath = new ArrayList();
        private ArrayList fileListName = new ArrayList();

        /// <summary>
        /// 文件路径
        /// </summary>
        public ArrayList FileListPath
        {
            get { return fileListPath; }
        }

        /// <summary>
        /// 文件名称
        /// </summary>
        public ArrayList FileListName
        {
            get { return fileListName; }
        }

        /// <summary>
        /// 构造函数并遍历文件夹获取文件名称,路径
        /// </summary>
        /// <param name="sourceDirectory">文件夹路径</param>
        /// <param name="nextFold">是否继续查找更深路径</param>
        public ControlFileClass(string sourceDirectory, bool nextFold)
        {
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            GetAllList(diSource, nextFold);
        }

        public void GetAllList(DirectoryInfo source, bool nextFold)
        {
            try
            {
                foreach (FileInfo fi in source.GetFiles())
                {
                    fileListPath.Add(fi.FullName);
                    fileListName.Add(fi.Name);
                }
                if (nextFold)//如果设置为可以向更深目录遍历则遍历
                {
                    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
                    {
                        GetAllList(diSourceSubDir, nextFold);
                    }
                }
                else//如果设置为不向更深目录遍历则直接用文件夹表示
                {
                    //遍历获取文件夹
                    foreach (DirectoryInfo d in source.GetDirectories())
                    {
                        fileListPath.Add(d.FullName);
                        fileListName.Add(d.Name);
                    }
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 判断一个路径是文件还是文件夹
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>true:文件夹,false:文件</returns>
        public static bool IsFolder(string fileNamePath)
        {
            FileInfo fileInfo = new FileInfo(fileNamePath);
            if ((fileInfo.Attributes & FileAttributes.Directory) != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除指定文件夹
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        public static void DeleteFolder(string dirPath)
        {
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件   
                }
                else
                {
                    DeleteFolder(d);//递归删除子文件夹   
                }
            }//end of for
            Directory.Delete(dirPath);//删除已空文件夹   
        }//end of DeleteFolder

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <param name="name">文件夹名</param>
        public static void CreateFolder(string dirPath, string name)
        {
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(dirPath + @"\" + name))
                {
                    Console.WriteLine("创建文件夹 " + name + " 失败,文件夹已经存在");
                    return;
                }
            }//end of for
            DirectoryInfo info = new DirectoryInfo(dirPath);
            info.CreateSubdirectory(name);
            //info.Parent.CreateSubdirectory(name);//可以在父目录生成文件夹,很方便

        }//end of CreateFolder

        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="dirPath">文件路径</param>
        /// <param name="name">文件名</param>
        public static void CreateFile(string dirPath, string name)
        {
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(dirPath + @"\" + name))
                {
                    Console.WriteLine("创建文件 " + name + " 失败,文件已经存在");
                    return;
                }
            }//end of for
            File.Create(dirPath + @"\" + name);
        }//end of CreateFile

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="dirPath">文件原始路径</param>
        /// <param name="tarPath">文件目标路径</param>
        /// <param name="name">文件名</param>
        public static void MoveFile(string dirPath, string tarPath, string name)
        {
            bool flag = false;
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(dirPath + @"\" + name))
                {
                    flag = true;
                }
            }//end of for

            if (!flag)
            {
                Console.WriteLine("目标文件 " + name + " 不存在");
                return;
            }

            File.Move(dirPath + @"\" + name, tarPath + @"\" + name);
        }//end of MoveFile

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="dirPath">文件原始路径</param>
        /// <param name="tarPath">文件目标路径</param>
        /// <param name="name">文件名</param>
        public static void CopyFile(string dirPath, string tarPath, string name)
        {
            bool flag = false;
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(dirPath + @"\" + name))
                {
                    flag = true;
                }
            }//end of for

            if (!flag)
            {
                Console.WriteLine("目标文件 " + name + " 不存在");
                return;
            }

            File.Copy(dirPath + @"\" + name, tarPath + @"\" + name);
        }//end of CopyFile

        /// <summary>
        /// 得到传递的路径中最后一个(文件名/文件夹名)
        /// </summary>
        /// <param name="dirPath">原始路径</param>
        /// <returns>string:文件名/文件夹名 </returns>
        public static string GetFileName(string dirpath)
        {
            for (int i = dirpath.Length - 1; i >= 0; i--)
            {
                if (dirpath[i] == '\\')
                {
                    dirpath = dirpath.Substring(i + 1, dirpath.Length - i - 1);
                    break;//不使用break要崩溃!!
                }
            }
            return dirpath;
        }//end of GetFileName

        /// <summary>
        /// 得到传递的路径中除开最后一个(文件名/文件夹名)的剩余路径
        /// 可以理解为得到自己的父目录的路径
        /// </summary>
        /// <param name="dirPath">原始路径</param>
        /// <returns>string:文件路径 </returns>
        public static string GetFolderPath(string dirpath)
        {
            for (int i = dirpath.Length - 1; i >= 0; i--)
            {
                if (dirpath[i] == '\\')
                {
                    dirpath = dirpath.Substring(0, i);
                    break;//不使用break要崩溃!!
                }
            }
            return dirpath;
        }//end of GetFolderPath

    }//end of class 
}//end of namespace 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
★: [D--目录指令 F--文件指令 A--混杂] ★: 指令名 别 描述 ☆: /ext F 修改文件扩展名 ☆: /extA F 递归子目录修改文件扩展名 ☆: /real F 修改文件真实名 ☆: /realA F 递归子目录修改文件真实名 ☆: /name F 修改文件全名 ☆: /nameA F 递归子目录修改文件全名 ☆: /move F 移动文件 ☆: /moveA F 递归子目录移动文件 ☆: /extractByA F 提取指定属性的文件 ☆: /extractByAA F 递归子目录提取指定属性的文件 ☆: /extractByT F 提取指定型的文件 ☆: /extractByTA F 递归子目录提取指定型的文件 ☆: /copyFromByA F 复制指定属性的文件 ☆: /copyFromByAA F 递归子目录复制指定属性的文件 ☆: /copyFromByT F 复制指定型的文件 ☆: /copyFromByTA F 递归子目录复制指定型的文件 ☆: /copyDir D 复制目录结构 ☆: /createFile F 创建指定数目的文件 ☆: /createDir D 提取指定数目的目录 ☆: /copyTo F 复制文件 ☆: /copyToA F 递归子目录复制文件 ☆: /copyToSubDir A 复制文件到子目录 ☆: /copyToSubDirA A 递归子目录复制文件到子目录 ☆: /dirName D 修改目录名 ☆: /dirNameA D 递归子目录修改目录名 ☆: /caseExt F 改变文件扩展名大小写 ☆: /caseExtA F 递归子目录改变文件扩展名大小写 ☆: /caseReal F 改变文件真实名大小写 ☆: /caseRealA F 递归子目录改变文件真实名大小写 ☆: /caseName F 改变文件全名大小写 ☆: /caseNameA F 递归子目录改变文件全名大小写 ☆: /caseDirName D 改变目录名大小写 ☆: /caseDirNameA D 递归子目录改变目录名大小写 ☆: /writeFilesToTxt F 将目录中的子文件列表写入文件,便于之后修改文件名 ☆: /nameFromFile F 根据文件中列出的文件列表修改当前目录中的文件名 ☆: /nameFromDir F 根据指定目录的文件列表修改当前目录中的文件名 ☆: /moveAvgToSubDir F 将指定目录的所有子文件平均的分配到当前的子目录中 ☆: /? A 显示帮助信息 ☆更详细指令信息,请输入具体指令并Enter查看.........
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值