在FileName中摆脱多个句点的问题

yea*_*mok 4 c# regex

我试图获取看起来像这样的文件名:
MAX_1.01.01.03.pdf看起来像Max_1010103.pdf.

目前我有这个代码:

public void Sanitizer(List<string> paths)
{
  string regPattern = (@"[~#&!%+{}]+");
  string replacement = " ";

  Regex regExPattern = new Regex(regPattern);
  Regex regExPattern2 = new Regex(@"\s{2,}");
  Regex regExPattern3 = new Regex(@"\.(?=.*\.)");
  string replace = "";

  var filesCount = new Dictionary<string, int>();
  dataGridView1.Rows.Clear();

  try
  {
    foreach (string files2 in paths)
    {
      string filenameOnly = System.IO.Path.GetFileName(files2);
      string pathOnly = System.IO.Path.GetDirectoryName(files2);
      string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
      sanitizedFileName = regExPattern2.Replace(sanitizedFileName, replacement);
      string sanitized = System.IO.Path.Combine(pathOnly, sanitizedFileName);

      if (!System.IO.File.Exists(sanitized))
      {
        DataGridViewRow clean = new DataGridViewRow();
        clean.CreateCells(dataGridView1);
        clean.Cells[0].Value = pathOnly;
        clean.Cells[1].Value = filenameOnly;
        clean.Cells[2].Value = sanitizedFileName;

        dataGridView1.Rows.Add(clean);
        System.IO.File.Move(files2, sanitized);
      }
      else 
      {
        if (filesCount.ContainsKey(sanitized))
        {
          filesCount[sanitized]++;
        }
        else
        {
          filesCount.Add(sanitized, 1);
          string newFileName = String.Format("{0}{1}{2}",
              System.IO.Path.GetFileNameWithoutExtension(sanitized),
              filesCount[sanitized].ToString(),
              System.IO.Path.GetExtension(sanitized));

          string newFilePath = System.IO.Path.Combine(
              System.IO.Path.GetDirectoryName(sanitized), newFileName);
          newFileName = regExPattern2.Replace(newFileName, replacement);
          System.IO.File.Move(files2, newFilePath);
          sanitized = newFileName;

          DataGridViewRow clean = new DataGridViewRow();
          clean.CreateCells(dataGridView1);
          clean.Cells[0].Value = pathOnly;
          clean.Cells[1].Value = filenameOnly;
          clean.Cells[2].Value = newFileName;

          dataGridView1.Rows.Add(clean);
        }

//HERE IS WHERE I AM TRYING TO GET RID OF DOUBLE PERIODS//
        if (regExPattern3.IsMatch(files2))
        {
          string filewithDoublePName = System.IO.Path.GetFileName(files2);
          string doublepPath = System.IO.Path.GetDirectoryName(files2);
          string name = System.IO.Path.GetFileNameWithoutExtension(files2);
          string newName = name.Replace(".", "");
          string filesDir = System.IO.Path.GetDirectoryName(files2);
          string fileExt = System.IO.Path.GetExtension(files2);
          string newPath = System.IO.Path.Combine(filesDir, newName+fileExt);

          DataGridViewRow clean = new DataGridViewRow();
          clean.CreateCells(dataGridView1);
          clean.Cells[0].Value =doublepPath;
          clean.Cells[1].Value = filewithDoublePName;
          clean.Cells[2].Value = newName;
          dataGridView1.Rows.Add(clean);
        }
      }
    }
    catch (Exception e)
    {
      throw;
      //errors.Write(e);
    }
  }
Run Code Online (Sandbox Code Playgroud)

我跑了这个而不是摆脱所有期间(减去文件扩展名之前的时间段),我得到的结果如下: MAX_1.0103.pdf

如果有多个时期,例如: Test....1.txt我得到以下结果: Test...1.txt

它似乎只能摆脱一个时期.我是正则表达式的新手,这是该项目的要求.任何人都可以帮我弄清楚我在做错了什么吗?

谢谢!

已编辑以显示代码中所做的更改

Chr*_*isF 12

为什么不使用这个Path:

string name = Path.GetFileNameWithoutExtension(yourPath);
string newName = name.Replace(".", "");
string newPath = Path.Combine(Path.GetDirectoryName(yourPath),
                              newName + Path.GetExtension(yourPath));
Run Code Online (Sandbox Code Playgroud)

为清楚起见,每个步骤分开

所以对于输入

"C:\用户\弗雷德\ MAX_1.01.01.03.pdf"

我得到了输出

"C:\用户\弗雷德\ MAX_1010103.pdf"

这是我所期待的.

如果我供应:

"C:\用户\ Fred.Flintstone\MAX_1.01.01.03.pdf"

我明白了:

"C:\用户\ Fred.Flintstone\MAX_1010103.pdf"

我还没想到,因为我没有处理路径的"DirectoryName"部分.

注意我错过了RegEx作为要求的一点.仍然坚持这个答案.