例如,我有这个搜索文件的功能:
public void Search(string strExtension,
DirectoryInfo di,
List<FileSystemInfo> pResult)
{
try
{
foreach (FileInfo fi in di.GetFiles())
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => label2.Text = fi.Name));
}
if (fi.Name == "MessageLog.xsl")
{
foreach (FileInfo fii in di.GetFiles())
{
if (fii.Extension == strExtension)
pResult.Add(fii);
}
if (InvokeRequired)
{
BeginInvoke(new Action(() => label4.Text = pResult.Count.ToString() + Environment.NewLine));
}
}
}
foreach (DirectoryInfo diChild in di.GetDirectories())
Search(strExtension, diChild, pResult);
}
catch (Exception e)
{
}
}
当使用这个小类进行搜索时:
public class MyItem
{
public FileSystemInfo fsi { get; set; }
public override string ToString()
{
return this.fsi.Name;
}
}
并在backGroundWorker完成事件中显示文件名:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
for (int i = 0; i < this.fsi.Count; i++)
{
this.listBox1.Items.Add(new MyItem() { fsi = fsi[i] });
}
}
listBox中的结果例如如下:
danny125645.xml
yaron576567.xml
这是没有目录的文件名。
有没有解析文件名所以没有数字?例如:
danny.xml
yaron.xml
答案 0 :(得分:1)
更新课程并进行更改:
public override string ToString()
{
return Regex.Replace(this.fsi.Name, @"[\d]", string.Empty);
}