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  

猜你喜欢

转载自blog.csdn.net/auccy/article/details/121407537