批量改文件名软件保留每次过滤扩展名,这样就不必每次都要手动新选择设置。

public class ConfigInfo
{
public List Extenstions { set; get; }
public ConfigInfo()
{
Extenstions = new List();
}
}
private static ConfigInfo getConfigInfo()
{
string json = Common.ConfigLord.GetConfig(new ConfigInfo().ToString());
ConfigInfo configInfo;
try
{
if (json == "") configInfo = new ConfigInfo();
else configInfo = JsonConvert.DeserializeObject(json);
}
catch
{
configInfo = new ConfigInfo();
}
return configInfo;
}
private bool setConfigInfo()
{
ConfigInfo configInfo = new ConfigInfo();
configInfo.Extenstions = _extensions;
string json = JsonConvert.SerializeObject(configInfo);
Spirit.Common.ConfigLord.SetConfig(configInfo.ToString(), json);
return true;
}
private void btnOK_Click(object sender, EventArgs e)
{
_extensions.Clear();
foreach (ListViewItem lvItem in lvExtensions.Items)
{
if (lvItem.Checked)
_extensions.Add(lvItem.Text);
}
if (_extensions.Count == 0)
{
MessageBox.Show("请至少选中一个文件扩展名称。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
NewDataTable = _dataTable.Clone();
setConfigInfo();
foreach (DataRow dr in _dataTable.Rows)
{
string fileName = dr["FileName"].ToString();
string extention = Path.GetExtension(fileName).ToLower().Trim();
if (_extensions.Contains(extention))
NewDataTable.ImportRow(dr);
}
DialogResult = DialogResult.OK;
}
private void FrmFilter_Load(object sender, EventArgs e)
{
lvExtensions.CheckBoxes = true;
lvExtensions.SmallImageList = new ImageList();
lvExtensions.SmallImageList.ImageSize = new Size(16, 16);
lvExtensions.SmallImageList.ColorDepth = ColorDepth.Depth32Bit;
lvExtensions.LargeImageList = new ImageList();
lvExtensions.FullRowSelect = true;
lvExtensions.HideSelection = false;
lvExtensions.View = View.Details;
_iconImageProvider = new IconImageProvider(lvExtensions.SmallImageList, lvExtensions.LargeImageList);
ColumnHeader ch = new ColumnHeader();
ch.Width = lvExtensions.Width - 5;
ch.Text = "扩展名称(后缀名称)";
lvExtensions.Columns.Add(ch);
List list = new List();
Dictionary dic = new Dictionary();
foreach (DataRow dr in _dataTable.Rows)
{
string fileName = dr["FileName"].ToString();
string extention = Path.GetExtension(fileName).ToLower().Trim();
string path = dr["Path"].ToString();
if (!dic.ContainsKey(extention))
{
list.Add(extention);
dic.Add(extention, path);
}
}
string[] arr = list.ToArray();
Array.Sort(arr);
ConfigInfo configInfo = getConfigInfo();
foreach (string extention in arr)
{
ListViewItem lvItem = new ListViewItem();
lvItem.ImageIndex = _iconImageProvider.GetIconImageIndex(dic[extention]);
lvItem.Text = extention;
if (configInfo.Extenstions.Contains(extention))
lvItem.Checked = true;
else
lvItem.Checked = false;
lvExtensions.Items.Add(lvItem);
}
}