Winform 文件插入工具

界面
在这里插入图片描述
使用图片
在这里插入图片描述

变量

/// <summary>
        /// 记录文件夹路径
        /// </summary>
        string _filePath = string.Empty;

        /// <summary>
        /// 文件夹下的 文件数量
        /// </summary>
        string _fileCount = string.Empty;

        /// <summary>
        /// 文件夹下的一级文件夹数量
        /// </summary>
        string _folderCount = string.Empty;

        /// <summary>
        /// 文件夹列表
        /// </summary>
        DirectoryInfo[] _folderList = null;

        /// <summary>
        /// 文件夹下的文件列表
        /// </summary>
        FileInfo[] _fileList = null;

        /// <summary>
        /// 目标文件夹路
        /// </summary>
        string _targetFolderPath = string.Empty;

        /// <summary>
        /// 目标文件夹文件列表
        /// </summary>
        DirectoryInfo[] _targetFolderList = null;

代码

 private void button6_Click(object sender, EventArgs e)
        {
            successCopytxt.Text = "";
            chuangjianwjjlab.Text = "";
            chuangjianwjjtxt.Text = "";

            if (string.IsNullOrEmpty(_filePath) || string.IsNullOrEmpty(_targetFolderPath))
            {
                MessageBox.Show("请按步骤选择文件夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!xmNhtrab.Checked && !fgfhrab.Checked)
            {
                MessageBox.Show("请选择原文件名类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (fgfhrab.Checked && string.IsNullOrEmpty(fengefutxt.Text))
            {
                MessageBox.Show("请填写分隔符号【@或#或$或!或+或-】等", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                fengefutxt.Focus();
                return;
            }
            if (fgfhrab.Checked && string.IsNullOrEmpty(duiyingbiaojitxt.Text))
            {
                MessageBox.Show("请填写【对应标记位置】。根据分隔符分开后的位置标记,填1或2或3或大于0整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                duiyingbiaojitxt.Focus();
                return;
            }
            //记录数据
            //找到对应的文件夹的数量
            int successCopyCount = 0;
            //未找到并创建的数量
            int createNewFolderCount = 0;
            string chuangjianwjj = string.Empty;

            //开始复制
            for (int i = 0; i < _fileList.Count(); i++)
            {
                string fileName = _fileList[i].Name;
                //去除文件扩展名
                string quchukuozhanming = fileName.Substring(0, fileName.LastIndexOf("."));
                //目标文件夹名称
                string targetFolderName = string.Empty;

                //如果选择了分列
                if (fgfhrab.Checked)
                {
                    string[] _ywjmfl = quchukuozhanming.Split(Convert.ToChar(fengefutxt.Text));
                    //如果填写的标记位置大于文件的分列数量,报错
                    if (Convert.ToInt32(duiyingbiaojitxt.Text.Trim()) - 1 > _ywjmfl.Count())
                    {
                        MessageBox.Show("填写的标记数错误,标记数最大值为文件分列的列数,最小为1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        duiyingbiaojitxt.Focus();
                        return;
                    }
                    //获得目标文件夹名称
                    targetFolderName = _ywjmfl[(Convert.ToInt32(duiyingbiaojitxt.Text.Trim())) - 1];
                }
                //原文件名
                else
                {
                    targetFolderName = quchukuozhanming;
                }
                //获得目标文件夹路径
                string targetFolderPath = _targetFolderPath + "\\" + targetFolderName;

                //判断目标文件夹是否 包含文件对应的文件夹
                if (Directory.Exists(targetFolderPath))
                {
                    CopyFile(fileName, targetFolderName);
                    successCopyCount++;
                }
                else
                {
                    Directory.CreateDirectory(targetFolderPath);
                    CopyFile(fileName, targetFolderName);
                    createNewFolderCount++;
                    chuangjianwjj = chuangjianwjj + fileName + ",\r\n";
                }
                successCopytxt.Text = "成功插入的文件数量【" + successCopyCount.ToString() + "】,无对应文件夹的文件数量【" + createNewFolderCount.ToString() + "】";
                if (createNewFolderCount > 0)
                {
                    chuangjianwjjlab.Text = "↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓异常文件记录↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓";
                    chuangjianwjjtxt.Text = chuangjianwjj.Substring(0, chuangjianwjj.LastIndexOf(","));
                }
            }

        }

CopyFile

 private void CopyFile(string fileName, string targetFolderName)
        {
            string wenjianxinmingcheng = wenjianxinmingchengtxt.Text.Trim();
            string ywjdz = _filePath + "\\" + fileName;
            if (string.IsNullOrEmpty(wenjianxinmingcheng))
            {
                string xwjdz = _targetFolderPath + "\\" + targetFolderName + "\\" + fileName;
                File.Copy(ywjdz, xwjdz, true);
            }
            else
            {
                string xwjdz = _targetFolderPath + "\\" + targetFolderName + "\\" + wenjianxinmingcheng + fileName.Substring(fileName.LastIndexOf("."));
                File.Copy(ywjdz, xwjdz, true);
            }
        }

感谢观看
来都来了,点个赞再走吧
点赞的都是嫖,不点赞的都是白嫖
微信搜索【猿的一日】IT老司机等你上车

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello-Rock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值