C#WinForm制作一个批量转化文件格式的小工具

前言

在生活中有时候会遇到批量转换格式的需求,一个个点太麻烦了,一个能够实现批量文件格式转换的工具非常有用。我这次就遇到了需要批量转换文件格式的需求,比如需要将doc文件转换为docx文件。

为这个小工具设定了两个基本功能,一个是选定一个文件夹后,将文件夹下的所有文件都转换为指定文件,另一个是批量选择文件将选中的文件转换为指定格式。

全部转换为指定格式的效果如下所示:

批量转换为指定格式的效果如下所示:

具体实现

界面的设计如下所示:

为了方便演示,使用的是原生控件,就是1个label、1个textbox、3个button。

确定按钮的代码如下:

vbnet 复制代码
  if(textBox1.Text != " " && textBox1.Text.StartsWith(".")) 
  {
      requiredExtension = textBox1.Text;
      MessageBox.Show($"已将需要的后缀名设定为:{requiredExtension}");
  }
  else
  {
      MessageBox.Show($"请设置正常的后缀名,以.开头");
  }
          

全部转换按钮的代码如下:

ini 复制代码
   if (requiredExtension != null)
   {
       // 创建一个FolderBrowserDialog对象
       FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
​
       // 设置对话框的标题
       folderBrowserDialog.Description = "选择需要转换格式的文件夹";
​
       // 设置默认的根文件夹,如果需要的话
       // folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
​
       // 显示文件夹选择对话框
       DialogResult result = folderBrowserDialog.ShowDialog();
​
       if (result == DialogResult.OK)
       {
           selectedFolderPath = folderBrowserDialog.SelectedPath;
           MessageBox.Show($"您选中的文件夹路径为:{selectedFolderPath}");
           // 调用 GetFiles 方法获取文件夹下的所有文件路径
           string[] files = Directory.GetFiles(selectedFolderPath);
           foreach (var file in files)
           {
               string fileExtension = Path.GetExtension(file);
               if (fileExtension != requiredExtension)
               {
                   // 构造新的文件路径,替换原来的后缀名
                   string newFilePath = Path.ChangeExtension(file, requiredExtension);
​
                   // 使用 File.Move 重命名文件
                   File.Move(file, newFilePath);
​
               }
           }
           MessageBox.Show($"转换文件格式成功,本次转化文件数为:{files.Length}");
       }
       else
       {
           MessageBox.Show("您本次没有选中文件夹!!!");
       }
   }
   else
   {
       MessageBox.Show("请先设定需要的后缀名!!!");
   }

批量转换的代码如下:

ini 复制代码
  if (requiredExtension != null)
  {
      OpenFileDialog openFileDialog = new OpenFileDialog();
      openFileDialog.Filter = "All Files (*.*)|*.*";
      openFileDialog.Multiselect = true;
      if (openFileDialog.ShowDialog() == DialogResult.OK)
      {
          // 获取选中的文件路径数组
          string[] selectedFiles = openFileDialog.FileNames;
          foreach (var file in selectedFiles)
          {
              string fileExtension = Path.GetExtension(file);
              if (fileExtension != requiredExtension)
              {
                  // 构造新的文件路径,替换原来的后缀名
                  string newFilePath = Path.ChangeExtension(file, requiredExtension);
​
                  // 使用 File.Move 重命名文件
                  File.Move(file, newFilePath);
​
              }
          }
          MessageBox.Show($"转换文件格式成功,本次转化文件数为:{selectedFiles.Length}");
      }
      else
      {
          MessageBox.Show("您本次没有选中任何文件!!!");
      }
​
  }
  else
  {
      MessageBox.Show("请先设定需要的后缀名!!!");
  }

最大的区别就是选中的是一个文件夹还是多个文件。

本示例全部源代码如下:

csharp 复制代码
    public partial class Form1 : Form
    {
        string requiredExtension;
        string selectedFolderPath;
        public Form1()
        {
            InitializeComponent();
        }
​
        private void label1_Click(object sender, EventArgs e)
        {
​
        }
​
        private void button3_Click(object sender, EventArgs e)
        {
            if(textBox1.Text != " " && textBox1.Text.StartsWith(".")) 
            {
                requiredExtension = textBox1.Text;
                MessageBox.Show($"已将需要的后缀名设定为:{requiredExtension}");
            }
            else
            {
                MessageBox.Show($"请设置正常的后缀名,以.开头");
            }
          
                       
            
        }
​
        private void button1_Click(object sender, EventArgs e)
        {
            if (requiredExtension != null)
            {
                // 创建一个FolderBrowserDialog对象
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
​
                // 设置对话框的标题
                folderBrowserDialog.Description = "选择需要转换格式的文件夹";
​
                // 设置默认的根文件夹,如果需要的话
                // folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
​
                // 显示文件夹选择对话框
                DialogResult result = folderBrowserDialog.ShowDialog();
​
                if (result == DialogResult.OK)
                {
                    selectedFolderPath = folderBrowserDialog.SelectedPath;
                    MessageBox.Show($"您选中的文件夹路径为:{selectedFolderPath}");
                    // 调用 GetFiles 方法获取文件夹下的所有文件路径
                    string[] files = Directory.GetFiles(selectedFolderPath);
                    foreach (var file in files)
                    {
                        string fileExtension = Path.GetExtension(file);
                        if (fileExtension != requiredExtension)
                        {
                            // 构造新的文件路径,替换原来的后缀名
                            string newFilePath = Path.ChangeExtension(file, requiredExtension);
​
                            // 使用 File.Move 重命名文件
                            File.Move(file, newFilePath);
​
                        }
                    }
                    MessageBox.Show($"转换文件格式成功,本次转化文件数为:{files.Length}");
                }
                else
                {
                    MessageBox.Show("您本次没有选中文件夹!!!");
                }
            }
            else
            {
                MessageBox.Show("请先设定需要的后缀名!!!");
            }
        }
​
        private void button2_Click(object sender, EventArgs e)
        {
            if (requiredExtension != null)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "All Files (*.*)|*.*";
                openFileDialog.Multiselect = true;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取选中的文件路径数组
                    string[] selectedFiles = openFileDialog.FileNames;
                    foreach (var file in selectedFiles)
                    {
                        string fileExtension = Path.GetExtension(file);
                        if (fileExtension != requiredExtension)
                        {
                            // 构造新的文件路径,替换原来的后缀名
                            string newFilePath = Path.ChangeExtension(file, requiredExtension);
​
                            // 使用 File.Move 重命名文件
                            File.Move(file, newFilePath);
​
                        }
                    }
                    MessageBox.Show($"转换文件格式成功,本次转化文件数为:{selectedFiles.Length}");
                }
                else
                {
                    MessageBox.Show("您本次没有选中任何文件!!!");
                }
​
            }
            else
            {
                MessageBox.Show("请先设定需要的后缀名!!!");
            }
        }
    }
}

如果你也对C#感兴趣,想查看更多文章,欢迎关注公众号DotNet学习交流~

相关推荐
yuhulkjv3353 小时前
Claude表格复制到word不再崩溃,AI导出鸭批量导出+格式无损一键搞定
人工智能·ai·c#·word·ai导出鸭
arbboter8 小时前
【网络工具】NetProxy网络代理用户手册
开发语言·网络·c#·网络代理·网络异常·代理工具
微小冷8 小时前
海康威视相机C#二次开发环境配置
数码相机·c#·.net·海康威视·相机开发
吴可可12312 小时前
C#重写机制与CAD图元开发要点
c#
A_nanda13 小时前
c#WPF开发常见问题
开发语言·c#·wpf
唐青枫13 小时前
别只会解析参数:C#.NET Spectre.Console.Cli 命令行工具实战详解
c#·.net
AI刀刀14 小时前
腾讯元宝粘贴到 word 格式混乱,AI 导出鸭一键规整排版
人工智能·c#·word·ai导出鸭
微小冷14 小时前
海康威视相机C#二次开发指图像采集
数码相机·c#·二次开发·机器视觉·海康威视·mvs·相机开发
rick97717 小时前
WinForms 父子窗体数据传递的进化之路:从参数注入到事件回传的完美闭环
c#
淡海水17 小时前
C#与常用数据结构源码剖析-全篇导览
开发语言·数据结构·unity·面试·职场和发展·c#