3 Star 7 Fork 1

清风来叙/YuReRenameTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
MainForm.cs 19.05 KB
一键复制 编辑 原始数据 按行查看 历史
清风来叙 提交于 2023-03-14 14:57 . 开放
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
using Microsoft.WindowsAPICodePack.Dialogs;
using RenameTools.Src;
using RenameTools.Src.Config;
using RenameTools.Src.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RenameTools
{
public partial class MainForm : Form
{
#if DEBUG
public static bool Debug = false;
#endif
public static bool IsOption = false;
public static NameConfig Config;
static bool Enable = true;
static List<FileFolderInfo> infos;
List<string> NewPathList;
public MainForm()
{
Enable = true;
infos = new List<FileFolderInfo>();
InitializeComponent();
InitConfig();
}
/// <summary>
/// 初始化界面
/// </summary>
private void InitConfig()
{
tbNewNameNumber.Text = "";
tbNewNameNumberLeft.Text = "";
tbNumberBegin.Text = "";
tbNewNameRaw.Text = "";
tbNewNameRawLeft.Text = "";
tbNewSuffix.Text = "";
tbRawField.Text = "";
tbNewField.Text = "";
cbRegularTrue.Checked = false;
cbCaseFalse.Checked = false;
rbRawName.Checked = true;
ClearAllRawFile();
Config_Changed(null,null);
tbNumberBegin.Hint = "0,00,...(默认:0)";
}
public void ClearAllRawFile()
{
infos.Clear();
lbRawNameList.Items.Clear();
lbNewNameList.Items.Clear();
}
/// <summary>
/// 批量重命名面板中点击数字方式或者原名方式切换
/// </summary>
/// <param name="sender">被点击的按钮</param>
/// <param name="e">触发的事件</param>
private void Rb_CheckedChanged(object sender, EventArgs e)
{
if(sender == rbNumberName)
{
// 数字方式命名
ChangNamedWay(true,false);
}
else if(sender == rbRawName)
{
// 原文件名方式命名
ChangNamedWay(false, false);
}
else if (sender == rbNewNameNumberLeft)
{
// 数字方式命名(数字在左)
ChangNamedWay(true, true);
}
else if (sender == rbNewNameRawLeft)
{
// 原文件名方式命名(原文件名在左)
ChangNamedWay(false, true);
}
Config_Changed(sender,e);
}
/// <summary>
/// 改变命名方式
/// </summary>
/// <param name="isNumber">是否是数字</param>
/// <param name="isLeft">是否是左边</param>
void ChangNamedWay(bool isNumber,bool isLeft)
{
tbNewNameNumberLeft.Enabled = isNumber && isLeft;
tbNewNameRawLeft.Enabled = !isNumber && isLeft;
tbNewNameNumber.Enabled = isNumber && !isLeft;
tbNewNameRaw.Enabled = !isNumber && !isLeft;
tbNumberBegin.Enabled = isNumber;
}
private void Btn_Click(object sender, EventArgs e)
{
if(Enable == false)
{
MessageBox.Show("正在执行操作,请稍等!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if(lbUpHelp == sender) {
MessageBox.Show("更改后按Tab键或者点击另外的控件使更改生效。", "帮助");
}
else
if(lbOrderTips == sender)
{
MessageBox.Show("在文件管理器中排好序,选好后点击第一个被选中的项然后拖入列表中即是有序的。","如何排序");
}else if(lbCleanInput == sender)
{
// 点击清除输入按钮
InitConfig();
}
else if (sender == lbSeeChange)
{
// 预览更改
RefreshNewName();
}
else if(btnAddFiles == sender)
{
DialogResult dr = MessageBox.Show("添加文件夹中的所有文件和文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
// 添加文件夹
var commonOpenFileDialog = new CommonOpenFileDialog
{
IsFolderPicker = true, //设置为true为选择文件夹,设置为false为选择文件
Title = "选择需要扫描的文件夹",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
var result = commonOpenFileDialog.ShowDialog();
if (result == CommonFileDialogResult.Ok)
{
string path = commonOpenFileDialog.FileName;
if (string.IsNullOrEmpty(path))
{
MessageBox.Show("扫描文件路径不能为空");
}
else
{
//指定的文件夹目录
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists == false)
{
MessageBox.Show("路径不存在!请重新输入");
}
else
{
DialogResult dialogResult = MessageBox.Show("是否添加文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
WorlkFileForm form = new WorlkFileForm();
form.WorlkFilesEvent += Form_WorlkFilesEvent;
form.Start(dir, dialogResult == DialogResult.Yes);
form.ShowDialog();
}
}
}
}
else
{
// 点击添加文件按钮
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "所有文件(*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
string[] files = dialog.FileNames;
AddFilesToSourceList(files);
}
}
}
else if(btnStart == sender)
{
// 开始按钮
Enable = false;
ExecuteForm exe = new ExecuteForm(infos);
exe.RenameOkEvent += Exe_ExecuteOkEvent;
exe.ShowDialog(this);
}
else if (sender == lbClean)
{
// 清除所有内容
DialogResult result = MessageBox.Show("是否清除列表中所有数据?", "清除", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
if(result == DialogResult.OK)
{
ClearAllRawFile();
}
}
}
private void AddListItem(ListBox listBox,object item)
{
if (listBox.InvokeRequired)
{
listBox.Invoke(
new Action<object>(n =>
{
listBox.Items.Add(n);
}
), item);
}
}
private void Form_WorlkFilesEvent(string fileOrDir)
{
string[] filesOrDirs = { fileOrDir };
AddFilesToSourceList(filesOrDirs,true);
}
private void Exe_ExecuteOkEvent(List<string> failure,List<string> NewPath)
{
this.NewPathList = NewPath;
Enable = true;
}
void RefreshNewName()
{
lbRawNameList.Items.Clear();
lbNewNameList.Items.Clear();
for (int i=0;i<infos.Count;i++)
{
infos[i].GetNewName(Config);
lbRawNameList.Items.Add(infos[i]);
lbNewNameList.Items.Add(infos[i].NewName);
}
}
/// <summary>
/// 添加文件到列表中
/// </summary>
/// <param name="array">文件列表</param>
/// <param name="isMt">是否是多線程添加</param>
private void AddFilesToSourceList(Array array, bool isMt = false)
{
if(array == null || array.Length == 0)
{
return;
}
int fail = 0;
foreach (object ff in array)
{
if (File.Exists(ff.ToString())
|| Directory.Exists(ff.ToString()))
{
#if DEBUG
Console.WriteLine("添加文件:" + ff.ToString());
#endif
AddFile(ff.ToString(),isMt);
}else{
fail++;
// Console.WriteLine(ff.ToString());
}
}
if (fail != 0)
{
MessageBox.Show("导入失败" + fail + "个", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LbRawNameList_DragDrop(object sender, DragEventArgs e)
{
object obj = e.Data.GetData(DataFormats.FileDrop);
if (obj is Array array)
{
AddFilesToSourceList(array);
#if DEBUG
Console.WriteLine("文件数组:" + array.Length+"个!");
#endif
}
}
private void LbRawNameList_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// 如果是文件
e.Effect = DragDropEffects.All;
}
else
{
// 如果不是文件
e.Effect = DragDropEffects.None;
}
}
private void LbRawNameList_DoubleClick(object sender, EventArgs e)
{
if(sender!= lbRawNameList)
{
return;
}
int index = lbRawNameList.SelectedIndex;
RemoveFile(index);
}
// 委托
public delegate void NameRuleChange(NameConfig config);
public event NameRuleChange NameRuleChangeEvent;
/// <summary>
/// 添加文件
/// </summary>
/// <param name="file">文件</param>
/// <param name="isMt">是否是多線程添加</param>
public void AddFile(string file,bool isMt = false)
{
if (IsOption)
{
return;
}
IsOption = true;
FileFolderInfo info = new FileFolderInfo(this,file, infos.Count);
infos.Add(info);
if (isMt)
{
AddListItem(lbNewNameList, info.GetNewName(Config));
AddListItem(lbRawNameList, info);
}
else
{
lbNewNameList.Items.Add(info.GetNewName(Config));
lbRawNameList.Items.Add(info);
}
#if DEBUG
if (Debug)
{
Console.WriteLine("添加文件");
}
#endif
IsOption = false;
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="index">文件索引</param>
public void RemoveFile(int index)
{
if(IsOption)
{
return;
}
IsOption = true;
if (index < infos.Count && index >= 0)
{
infos.RemoveAt(index);
lbNewNameList.Items.RemoveAt(index);
lbRawNameList.Items.RemoveAt(index);
for (int i = index;i< infos.Count; i++)
{
infos[i].SetIndex(i);
lbNewNameList.Items[i] = infos[i].ToString(true, true);
lbRawNameList.Items[i] = infos[i].ToString();
}
#if DEBUG
if (Debug)
{
Console.WriteLine("移除文件");
}
#endif
}
IsOption = false;
}
/// <summary>
/// 只允许输入0-9的字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InputNumber_KeyPress(object sender, KeyPressEventArgs e)
{
//这是允许输入退格键
if (e.KeyChar != '\b')
{
//这是允许输入0-9数字
if ((e.KeyChar < '0') || (e.KeyChar > '9'))
{
e.Handled = true;
}
}
}
/// <summary>
/// 文件名合法输入校验
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InputFileName_KeyPress(object sender, KeyPressEventArgs e)
{
// 文件名不能包含如下字符
if (
e.KeyChar == '\\'||
e.KeyChar == '/' ||
e.KeyChar == ':' ||
e.KeyChar == '*' ||
e.KeyChar == '?' ||
e.KeyChar == '"' ||
e.KeyChar == '<' ||
e.KeyChar == '>' ||
e.KeyChar == '|'
)
{
e.Handled = true;
}
}
/// <summary>
/// 忽略大小写和正则的改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cb_CheckedChanged(object sender, EventArgs e)
{
if(sender == cbCaseFalse)
{
if (cbCaseFalse.Checked)
{
cbRegularTrue.Checked = false;
}
}
else if (sender == cbRegularTrue)
{
if (cbRegularTrue.Checked)
{
cbCaseFalse.Checked = false;
}
}
Config_Changed(sender,e);
}
private void Config_Changed(object sender, EventArgs e)
{
if(Config == null)
{
Config = new NameConfig();
}
bool isChange = false;
if (Config.caseTrue != cbCaseFalse.Checked)
{
isChange = true;
Config.caseTrue = cbCaseFalse.Checked;
}
if (Config.regularTrue != cbRegularTrue.Checked)
{
isChange = true;
Config.regularTrue = cbRegularTrue.Checked;
}
if (Config.findText != tbRawField.Text)
{
isChange = true;
Config.findText = tbRawField.Text;
}
if (Config.targetText != tbNewField.Text)
{
isChange = true;
Config.targetText = tbNewField.Text;
}
if (Config.extend != tbNewSuffix.Text)
{
isChange = true;
Config.extend = tbNewSuffix.Text;
}
RenameMode mode = RenameMode.None;
string p1 = null, p2 = null;
if (rbNumberName.Checked)
{
// *** + 数字
mode = RenameMode.TextNumber;
p1 = tbNewNameNumber.Text;
if (tbNumberBegin.IsHint)
{
p2 = "0";
}
else
{
p2 = tbNumberBegin.Text;
}
}
else if (rbNewNameNumberLeft.Checked)
{
// 数字 + ***
mode = RenameMode.NumberText;
p1 = tbNewNameNumberLeft.Text;
if (tbNumberBegin.IsHint)
{
p2 = "0";
}
else
{
p2 = tbNumberBegin.Text;
}
}
else if (rbRawName.Checked)
{
// *** + 原名
mode = RenameMode.TextName;
p1 = tbNewNameRaw.Text;
}
else if (rbNewNameRawLeft.Checked)
{
// 原名 + ***
mode = RenameMode.NameText;
p1 = tbNewNameRawLeft.Text;
}
if ((Config.mode != mode) && (mode != RenameMode.None))
{
isChange = true;
Config.mode = mode;
}
if (Config.p1 != p1)
{
isChange = true;
Config.p1 = p1;
}
if (Config.p2 != p2)
{
isChange = true;
Config.p2 = p2;
}
if (isChange)
{
#if DEBUG
Console.WriteLine("Mode : " + Config.mode);
#endif
NameRuleChangeEvent?.Invoke(Config);
RefreshNewName();
}
}
private void List_SelectedIndexChanged(object sender, EventArgs e)
{
if(sender is ListBox lb)
{
tbName.Text = lb.Items[lb.SelectedIndex].ToString();
}
}
private void TextBox_Leave(object sender, EventArgs e)
{
Config_Changed(sender,e);
}
/// <summary>
/// 重新引入文件,用于改名后重新添加到待改名位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Reload_Click(object sender, EventArgs e)
{
if(this.NewPathList == null)
{
MessageBox.Show("無需重新引入","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
// 清除所有內容
ClearAllRawFile();
AddFilesToSourceList(this.NewPathList.ToArray());
this.NewPathList = null;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Minuhy/yu-re-rename-tool.git
git@gitee.com:Minuhy/yu-re-rename-tool.git
Minuhy
yu-re-rename-tool
YuReRenameTool
main

搜索帮助