代码编写:
- 引用COM组件Microsoft Shell Controls And Automation,设置“嵌入互操作类型”为“否”
- 拖控件
-
using System.IO; using System; using System.Diagnostics; using Shell32; using System.Drawing; namespace AudioTools { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (folderBrowserDialogSelectAudioFold.ShowDialog() == DialogResult.OK || folderBrowserDialogSelectAudioFold.ShowDialog() == DialogResult.Yes) { if(folderBrowserDialogSelectAudioFold.SelectedPath.Length > 0) { txtAudioPath.Text = folderBrowserDialogSelectAudioFold.SelectedPath; //Trace.WriteLine(folderBrowserDialogSelectAudioFold.SelectedPath); DirectoryInfo root = new DirectoryInfo(folderBrowserDialogSelectAudioFold.SelectedPath); this.lvAudioList.BeginUpdate(); int index = 0; foreach (FileInfo f in root.GetFiles()) { //Trace.WriteLine(f.Name); ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = index; lvi.Text = (++index).ToString(); lvi.SubItems.Add(f.Name); this.lvAudioList.Items.Add(lvi); } this.lvAudioList.EndUpdate(); } } } private void frmMain_Load(object sender, EventArgs e) { Trace.WriteLine("frmMain_Load"); ColumnHeader col1 = new ColumnHeader(); col1.Text = "编号"; col1.Width = 60; col1.TextAlign = HorizontalAlignment.Left; this.lvAudioList.Columns.Add(col1); ColumnHeader col2 = new ColumnHeader(); col2.Text = "名称"; col2.Width = 240; col2.TextAlign = HorizontalAlignment.Left; this.lvAudioList.Columns.Add(col2); } private void btnRename_Click(object sender, EventArgs e) { try { if (txtAudioPath.Text.Trim() != "") { DirectoryInfo root = new DirectoryInfo(txtAudioPath.Text.Trim()); this.lvAudioList.Items.Clear(); this.lvAudioList.BeginUpdate(); int index = 0; ShellClass sh = new ShellClass(); string fileName, artist, title, name, type; Folder dir; FolderItem item; foreach (FileInfo f in root.GetFiles()) { //Trace.WriteLine(f.Name); fileName = txtAudioPath.Text.Trim() + "\\" + f.Name; dir = sh.NameSpace(Path.GetDirectoryName(fileName)); item = dir.ParseName(Path.GetFileName(fileName)); artist = dir.GetDetailsOf(item, 13); title = dir.GetDetailsOf(item, 21); name = dir.GetDetailsOf(item, 0); type = dir.GetDetailsOf(item, 164); //测试文件拓展名的索引 //for (int i = 0; i < 200; i++) //{ // Trace.WriteLine(i.ToString() + dir.GetDetailsOf(item, i)); //} string newFileName = ""; if (0 == artist.Length || artist == "") { newFileName = name; return; } else { newFileName = artist + "-" + title; } ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = index; lvi.Text = (++index).ToString(); lvi.SubItems.Add(newFileName); this.lvAudioList.Items.Add(lvi); FileInfo fi = new FileInfo(fileName); fi.MoveTo(Path.Combine(txtAudioPath.Text.Trim() + "\\" + newFileName + type)); } this.lvAudioList.EndUpdate(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
- 下载链接:https://pan.baidu.com/s/1S8OHPGA5GVl4NUJoJaxnEw
提取码:axzi