【问题标题】:Extract a pattern from a group of filenames and place into listbox从一组文件名中提取模式并放入列表框中
【发布时间】:2018-12-07 20:38:48
【问题描述】:

我有一个文件夹,里面有很多这样的文件:

2016-01-02-03-abc.txt
2017-01-02-03-defjh.jpg
2018-05-04-03-hij.txt
2022-05-04-03-klmnop.jpg

我需要从每组文件名中提取模式。 例如,我需要放在列表中的前两个文件中的模式 01-02-03。我还需要将模式 05-04-03 放在同一个列表中。因此,我的列表将如下所示:

01-02-03
05-04-03

这是我目前所拥有的。我可以成功删除字符,但将一个模式实例重新添加到列表中超出了我的工资等级:

        public void GetPatternsToList()
    {
        //Get all filenames with characters removed and place in listbox.

        List<string> files = new List<string>(Directory.EnumerateFiles(folderBrowserDialog1.SelectedPath));
        foreach (var file in files)
        {
            var removeallbeforefirstdash = file.Substring(file.IndexOf("-") + 1); // removes everthing before the dash in the filename
            var finalfile = removeallbeforefirstdash.Substring(0,removeallbeforefirstdash.LastIndexOf("-")); // removes everything after dash in name -- will crash if file without dash is in folder (not sure how to fix this either)

            string[] array = finalfile.ToArray(); // I need to do the above with each file in the list and then place it back in an array to display in a listbox
            List<string> filesList = array.ToList();
            listBox1.DataSource = filesList;
        }

    }

【问题讨论】:

    标签: c# arrays listbox enumerate


    【解决方案1】:

    你可以这样做:

    public void GetPatternsToList()
    {
        var files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
    
        var patterns = new HashSet<string>();
    
        foreach (var file in files)
        {
            var splitFileName = file.Split('-').Skip(1).Take(3);
            var joinedFileName = string.Join("-", splitFileName);
    
            if(!string.IsNullOrEmpty(joinedFileName)
                patterns.Add(joinedFileName);
        }
    
        listBox1.DataSource = patterns;
    }
    

    我使用了HashSet&lt;string&gt; 以避免向数据源添加重复的模式。

    一些与您的问题无关的评论,但您的代码一般:

    • 我会将 SelectedPath 作为字符串传递给方法
    • 我会让方法返回 HashSet
    • 如果你实现了上述方法,也请相应地命名方法

    以上所有这些对您来说当然是可选的,但会提高您的代码质量。

    【讨论】:

      【解决方案2】:

      试试这个:

      public void GetPatternsToList()
      {
          List<string> files = new List<string>(Directory.EnumerateFiles(folderBrowserDialog1.SelectedPath));
          List<string> resultFiles = new List<string>();
          foreach (var file in files)
          {
              var removeallbeforefirstdash = file.Substring(file.IndexOf("-") + 1); // removes everthing before the dash in the filename
              var finalfile = removeallbeforefirstdash.Substring(0, removeallbeforefirstdash.LastIndexOf("-")); // removes everything after dash in name -- will crash if file without dash is in folder (not sure how to fix this either)
              resultFiles.Add(finalfile);
          }
          listBox1.DataSource = resultFiles.Distinct().ToList();
      }
      

      【讨论】:

      • 为什么不只添加到resultFilesif (!resultFiles.Contains(finalfile))
      • @SamW 我同意,它可以使用。我写的第一个是我想到的。
      猜你喜欢
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多