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学习交流~

相关推荐
影寂ldy3 小时前
C# try-catch 异常处理全套笔记
服务器·数据库·c#
TeamDev4 小时前
JxBrowser 9.3.0 版本发布啦!
java·后端·c#·混合应用·jxbrowser·浏览器控件·异步媒体设备
梦帮科技4 小时前
UE5 GAS 实战:用 Gameplay Ability System 搭建「赛博修真」境界与技能体系
c++·人工智能·python·ue5·c#
北域码匠8 小时前
RIPEMD-128哈希算法深度解析
c#·密码学·哈希算法·加密算法·消息摘要·ripemd-128·原生实现
csdn_aspnet10 小时前
C# 截取或匹配字符串内包含指定字符
c#·字符串·正则·string·匹配·截取
hez20104 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉9 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫10 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫11 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62512 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net