-1

例如我有这个搜索文件的功能:

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
4

1 回答 1

1

更新您的课程并更改:

public override string ToString()
{
    return Regex.Replace(this.fsi.Name, @"[\d]", string.Empty);
}
于 2012-08-26T02:02:34.893 回答