title: Replace part of a filename in C#
body: <p>I have a folder with <code>.pdf</code> files. In the names of most files I want to replace specific string with another string. </p>
  
  <p>Here's what I've written.</p>
  
  <pre><code>  private void btnGetFiles_Click(object sender, EventArgs e)
      {
          string dir = tbGetFIles.Text;
          List&lt;string&gt; FileNames = new List&lt;string&gt;();
  
          DirectoryInfo DirInfo = new DirectoryInfo(dir);
  
          foreach (FileInfo File in DirInfo.GetFiles())
          {
              FileNames.Add(File.Name);       
          }
  
          lbFileNames.DataSource = FileNames;
      }
  </code></pre>
  
  <p>Here I extract all file names in List Box.</p>
  
  <pre><code>    private void btnReplace_Click(object sender, EventArgs e)
      {
          string strReplace = tbReplace.Text; // The existing string
          string strWith = tbWith.Text; // The new string
  
          string dir = tbGetFIles.Text;
          DirectoryInfo DirInfo = new DirectoryInfo(dir);
          FileInfo[] names = DirInfo.GetFiles();
  
  
          foreach (FileInfo f in names)
          {
              if(f.Name.Contains(strReplace))
              {
                  f.Name.Replace(strReplace, strWith);
              }
  
          }
  </code></pre>
  
  <p>And here I want to do the replacing, but something is going wrong. What?</p>