-1

For example i have this function that search for files:

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)
            {
            }
        }

When the search is over im using this small class :

public class MyItem
        {
            public FileSystemInfo fsi { get; set; }
            public override string ToString()
            {
                return this.fsi.Name;
            }
        }

And show in the backGroundWorker completed event the files names:

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] });
            }


        }

The result in the listBox is for example like this:

danny125645.xml

yaron576567.xml

This are file names without the directories.

Is there anyway to parse the file name so it will be without the numbers ? For example:

danny.xml

yaron.xml
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 5
    [How to remove numbers from a string](http://stackoverflow.com/questions/1657282/how-to-remove-numbers-from-string-in-regex-replace) – Zaid Masud Aug 26 '12 at 02:00

1 Answers1

1

Update your class and change:

public override string ToString()
{
    return Regex.Replace(this.fsi.Name, @"[\d]", string.Empty);
}
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284