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

相关推荐
friklogff1 小时前
【无标题】云端之C#:全面解析6种云服务提供商的SDK
开发语言·flask·c#
c#上位机2 小时前
C#事件的用法
java·javascript·c#
chnyi6_ya2 小时前
一些写leetcode的笔记
笔记·leetcode·c#
IT规划师3 小时前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
时光追逐者3 小时前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
friklogff4 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
__water12 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water12 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water13 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
__water15 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能