C# 统改文件名软件

在winform中拖四个button控件、两个textbox控件、一个combobox控件、一个openfiledialog控件。如图

 

 下面为代码部分:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
  public partial class changefilename : Form
{
public changefilename()
{
  InitializeComponent();
}
  List<string> oldf = new List<string>();
  List<string> newf = new List<string>();
  private void button1_Click(object sender, EventArgs e)
  {

  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.Multiselect = true; // 可以选择多个选项
  openFileDialog.InitialDirectory = "e:\\";//注意这里写路径时要用c:\\而不是c:\
  openFileDialog.Filter = "文本文件|*.jpg|所有文件|*.*";
  openFileDialog.RestoreDirectory = true;
  openFileDialog.FilterIndex = 1;
  DialogResult result = openFileDialog.ShowDialog();
  if (result == DialogResult.OK)
  {
    foreach (string path in openFileDialog.FileNames)
    {
      textBox1.Text += path + "\r\n";
    }
  }
  }

  private void button2_Click(object sender, EventArgs e)
  {
  if (comboBox1.Text== "选择后缀名")
  {
    MessageBox.Show("请选择或填入正确的后缀名!");
  }
  else
  {
    string cbtext = comboBox1.Text;
    oldf.Clear();
    for (int i = 0; i < textBox1.Lines.Length; i++)
    {
      string path1 = textBox1.Lines[i].Replace("\\", "\\\\");
      if (path1 != "") { oldf.Add(path1); }
    }

    string s1 = oldf[0].Substring(0, oldf[0].LastIndexOf("\\") + 1);
    string s2 = s1.Replace("\\", "\\\\");

    newf.Clear();
    for (int i = 0; i < textBox2.Lines.Length; i++)
    {
      if (textBox2.Lines[i] != "")
      { newf.Add(s2 + textBox2.Lines[i] + cbtext); }
    }
    if (oldf.Count() == newf.Count())
    {
      for (int m = 0; m < oldf.Count(); m++)
      {
        File.Move(oldf[m], newf[m]); //注意这里参数变量中要用\\而不是\
      }
    MessageBox.Show("改名成功");
    }
    else
    {
        MessageBox.Show("数量不相同,请检查!");
    }
  }
  }

  private void button3_Click(object sender, EventArgs e)
  {
    textBox1.Clear();
  }

  private void changefilename_Load(object sender, EventArgs e)
  {
  comboBox1.Items.Add(".txt");
  comboBox1.Items.Add(".jpg");
  comboBox1.Items.Add(".png");
  comboBox1.Items.Add(".mp3");
  comboBox1.Items.Add(".mp4");

  }

  private void button4_Click(object sender, EventArgs e)
  {
  textBox2.Clear();
  }
 }
}

posted @   杨咩咩plus  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示