Files
font-preview/FontPreview/FontRenameDialog.cs
T

284 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace FontPreview;
internal class FontRenameDialog : Form
{
private List<FontItem> items;
public bool Renamed;
private TextBox txtPrefix;
private TextBox txtSuffix;
private TextBox txtFind;
private TextBox txtReplace;
private CheckBox chkSeq;
private NumericUpDown numSeq;
private ListView lv;
public FontRenameDialog(List<FontItem> list)
{
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Expected O, but got Unknown
items = list;
((Control)this).Text = "批量重命名(仅改文件名)";
((Control)this).Width = 820;
((Control)this).Height = 620;
((Form)this).StartPosition = (FormStartPosition)4;
((Control)this).Font = new Font("Microsoft YaHei UI", 9f);
Build();
}
private void Build()
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0007: Expected O, but got Unknown
//IL_0023: Unknown result type (might be due to invalid IL or missing references)
//IL_002d: Expected O, but got Unknown
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0047: Expected O, but got Unknown
//IL_0066: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Expected O, but got Unknown
//IL_0087: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Expected O, but got Unknown
//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Expected O, but got Unknown
//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
//IL_00d3: Expected O, but got Unknown
//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
//IL_00fc: Expected O, but got Unknown
//IL_0190: Unknown result type (might be due to invalid IL or missing references)
//IL_019a: Expected O, but got Unknown
//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
//IL_01c9: Expected O, but got Unknown
//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
//IL_01f4: Expected O, but got Unknown
//IL_0277: Unknown result type (might be due to invalid IL or missing references)
//IL_027e: Expected O, but got Unknown
//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
//IL_02b9: Expected O, but got Unknown
//IL_0336: Unknown result type (might be due to invalid IL or missing references)
//IL_0340: Expected O, but got Unknown
//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
//IL_03e8: Expected O, but got Unknown
Label val = new Label();
((Control)val).Text = "前缀";
((Control)val).SetBounds(12, 14, 40, 22);
txtPrefix = new TextBox();
((Control)txtPrefix).SetBounds(52, 11, 110, 24);
Label val2 = new Label();
((Control)val2).Text = "后缀";
((Control)val2).SetBounds(170, 14, 40, 22);
txtSuffix = new TextBox();
((Control)txtSuffix).SetBounds(210, 11, 110, 24);
Label val3 = new Label();
((Control)val3).Text = "查找";
((Control)val3).SetBounds(330, 14, 40, 22);
txtFind = new TextBox();
((Control)txtFind).SetBounds(370, 11, 100, 24);
Label val4 = new Label();
((Control)val4).Text = "替换";
((Control)val4).SetBounds(480, 14, 40, 22);
txtReplace = new TextBox();
((Control)txtReplace).SetBounds(520, 11, 100, 24);
((Control)this).Controls.Add((Control)(object)val);
((Control)this).Controls.Add((Control)(object)txtPrefix);
((Control)this).Controls.Add((Control)(object)val2);
((Control)this).Controls.Add((Control)(object)txtSuffix);
((Control)this).Controls.Add((Control)(object)val3);
((Control)this).Controls.Add((Control)(object)txtFind);
((Control)this).Controls.Add((Control)(object)val4);
((Control)this).Controls.Add((Control)(object)txtReplace);
chkSeq = new CheckBox();
((Control)chkSeq).Text = "自动编号避免重名";
((Control)chkSeq).SetBounds(12, 44, 160, 24);
Label val5 = new Label();
((Control)val5).Text = "起始";
((Control)val5).SetBounds(180, 48, 34, 22);
numSeq = new NumericUpDown();
numSeq.Minimum = 1m;
numSeq.Maximum = 9999m;
numSeq.Value = 1m;
((Control)numSeq).SetBounds(216, 44, 60, 24);
((Control)this).Controls.Add((Control)(object)chkSeq);
((Control)this).Controls.Add((Control)(object)val5);
((Control)this).Controls.Add((Control)(object)numSeq);
Button val6 = new Button();
((Control)val6).Text = "生成预览";
((Control)val6).SetBounds(300, 42, 96, 28);
((Control)val6).Click += delegate
{
Preview();
};
Button val7 = new Button();
((Control)val7).Text = "执行重命名";
((Control)val7).BackColor = Color.FromArgb(196, 82, 43);
((Control)val7).ForeColor = Color.White;
((ButtonBase)val7).FlatStyle = (FlatStyle)0;
((Control)val7).SetBounds(696, 42, 110, 28);
((Control)val7).Click += delegate
{
DoRename();
};
((Control)this).Controls.Add((Control)(object)val6);
((Control)this).Controls.Add((Control)(object)val7);
lv = new ListView();
lv.View = (View)1;
lv.FullRowSelect = true;
lv.GridLines = true;
lv.Columns.Add("原名", 340);
lv.Columns.Add("新名", 340);
lv.Columns.Add("状态", 100);
((Control)lv).SetBounds(12, 80, 796, 460);
((Control)this).Controls.Add((Control)(object)lv);
Button val8 = new Button();
((Control)val8).Text = "关闭";
val8.DialogResult = (DialogResult)2;
((Control)val8).SetBounds(696, 550, 110, 30);
((Control)this).Controls.Add((Control)(object)val8);
((Form)this).CancelButton = (IButtonControl)(object)val8;
}
private void Preview()
{
lv.BeginUpdate();
lv.Items.Clear();
int num = (int)numSeq.Value;
HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
for (int i = 0; i < items.Count; i++)
{
FontItem fontItem = items[i];
string text = Path.GetFileNameWithoutExtension(fontItem.Name);
string extension = Path.GetExtension(fontItem.Name);
if (((Control)txtFind).Text.Length > 0)
{
text = text.Replace(((Control)txtFind).Text, ((Control)txtReplace).Text);
}
string text2 = ((Control)txtPrefix).Text + text + ((Control)txtSuffix).Text + extension;
string text3 = "";
if (string.Equals(text2, fontItem.Name, StringComparison.OrdinalIgnoreCase))
{
text3 = "无变化";
ListViewItem val = lv.Items.Add(fontItem.Name);
val.SubItems.Add(text2);
val.SubItems.Add(text3);
continue;
}
if (hashSet.Contains(text2))
{
if (chkSeq.Checked)
{
do
{
text2 = ((Control)txtPrefix).Text + text + ((Control)txtSuffix).Text + "_" + num.ToString("000") + extension;
num++;
}
while (hashSet.Contains(text2));
hashSet.Add(text2);
text3 = "已编号";
}
else
{
text3 = "重名(未处理)";
}
}
else
{
hashSet.Add(text2);
}
ListViewItem val2 = lv.Items.Add(fontItem.Name);
val2.SubItems.Add(text2);
val2.SubItems.Add(text3);
if (text3 == "重名(未处理)")
{
val2.ForeColor = Color.Red;
}
}
lv.EndUpdate();
}
private void DoRename()
{
//IL_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_029f: Unknown result type (might be due to invalid IL or missing references)
if (lv.Items.Count == 0)
{
MessageBox.Show((IWin32Window)(object)this, "请先点击「生成预览」", "提示");
return;
}
int num = 0;
int num2 = 0;
List<string> list = new List<string>();
for (int i = 0; i < lv.Items.Count; i++)
{
string path = items[i].Path;
string text = lv.Items[i].SubItems[1].Text;
string text2 = lv.Items[i].SubItems[2].Text;
if (text2 == "无变化" || text2 == "重名(未处理)")
{
if (text2 == "重名(未处理)")
{
num2++;
}
continue;
}
string fileName = Path.GetFileName(path);
if (string.Equals(fileName, text, StringComparison.OrdinalIgnoreCase))
{
num++;
continue;
}
try
{
string directoryName = Path.GetDirectoryName(path);
string text3 = (string.IsNullOrEmpty(directoryName) ? text : Path.Combine(directoryName, text));
if (File.Exists(text3))
{
num2++;
list.Add(path);
}
else
{
File.Move(path, text3);
num++;
}
}
catch (Exception)
{
num2++;
list.Add(path);
}
}
if (num > 0)
{
Renamed = true;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("重命名完成:成功 " + num + ",失败 " + num2 + "\r\n");
if (list.Count > 0 && list.Count <= 20)
{
stringBuilder.Append("失败项:\r\n");
foreach (string item in list)
{
stringBuilder.Append(" " + Path.GetFileName(item) + "\r\n");
}
}
if (list.Count > 20)
{
stringBuilder.Append("(失败项过多,仅显示前 20 条)\r\n");
}
MessageBox.Show((IWin32Window)(object)this, stringBuilder.ToString(), "批量重命名", (MessageBoxButtons)0, (MessageBoxIcon)64);
}
}