C#实现文件名称批量添加后缀

这个文件名称批量添加后缀工具同事使用还需要安装Python,因此调整为纯C#实现

代码如下:需要添加Microsoft.VisualBasic引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
using MessageBox = System.Windows.MessageBox;
using Microsoft.VisualBasic.Devices;

namespace RenameFiles
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static string m_suffixStr = "";
        static string m_dstPath = "";
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (m_dstPath.Length==0)
            {
                System.Windows.Forms.MessageBox.Show("重命名路径不能为空");
                return;
            }

            string goalPath = System.IO.Directory.GetCurrentDirectory()+"\\newFiles";
            Computer myCom = new Computer();
            if (myCom.FileSystem.DirectoryExists(goalPath))
            {
                DirectoryInfo rootDir = new DirectoryInfo(goalPath);
                rootDir.Delete(true);
            }
            myCom.FileSystem.CreateDirectory(goalPath);
            try
            {
                //遍历路径下所有文件,重命名
                DirectoryInfo dirInfo = new DirectoryInfo(m_dstPath);
                FileInfo[] files = dirInfo.GetFiles();
                foreach (FileInfo file in files)
                {
                    int pos = file.Name.IndexOf(".");
                    if (pos > 0)
                    {
                        string exaStr = file.Name.Substring(pos, file.Name.Length- pos);
                        string realName = file.Name.Substring(0, pos)+ m_suffixStr+ exaStr;
                        myCom.FileSystem.CopyFile(m_dstPath + "\\" + file.Name, goalPath +"\\"+ realName);
                    }
                }

                MessageBox.Show("rename files success");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        /// <summary>
        /// 放下文件获取路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void onDropFile(object sender, System.Windows.DragEventArgs e)
        {
            var files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
            if (files == null)
            {
                return;
            }
            for (int i = 0; i < files.Length; i++)
            {
                label_floder.Content = files[i];
                m_dstPath= files[i];
            }
        }
        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
		{
            m_suffixStr = textBox.Text;
        }
	}
}

工具地址:文件名批量添加后缀工具

 提取码:hmoa  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值