【问题标题】:c# String Query - Getting latest file revisionc# String Query - 获取最新的文件版本
【发布时间】:2019-02-26 17:08:25
【问题描述】:

我正在编写一个表单应用程序,它显示 Datagridview 目录中的所有 PDF 文件。 现在文件名格式通常是12740-250-B-File Name(所以基本上是XXXXXX-XXX-X-XXXXXXX)。 所以第一个数字是项目号,破折号后面的第二个数字是序列号-字母是文件的修订版。

我想要一个按钮,按下时它会找到具有相同序列号的文件(XXXXX-Series No - Revision - XXXXXX)并显示最新的版本,它将是最大的字母,所以在 12763 之间-200-A-HelloWorld 和 12763-200-B-HelloWorld 我希望 12763-200-B-HelloWorld 成为我的查询结果。

这是我目前得到的:

private void button1_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
    String[] files = Directory.GetFiles(@"M:\Folder Directory","*.pdf*", SearchOption.AllDirectories);
    DataTable table = new DataTable();
    table.Columns.Add("File Name");

    for (int i = 0; i < files.Length; i++)
    {
        FileInfo file = new FileInfo(files[i]);
        table.Rows.Add(file.Name);

    }
    dataGridView1.DataSource = table;
}

提前致谢。

注意: 最后,最新版本的文件将被插入到 Excel 电子表格中。

【问题讨论】:

    标签: c# database string filenames


    【解决方案1】:

    假设您的收藏是一个文件名列表,它是 Directory.GetFiles("");下面的 linq 将起作用。为了使下面的工作,您需要确定文件格式,因为拆分对特定文件格式非常敏感。

    var seriesNumber = "200";
    var files = new List<string> { "12763-200-A-HelloWorld", "12763-200-B-HelloWorld" };
    
    var matching = files.Where(x => x.Split('-')[1] == seriesNumber)
                        .OrderByDescending(x => x.Split('-')[2])
                        .FirstOrDefault();
    

    结果

    匹配:“12763-200-B-HelloWorld”

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法:

      string dirPath = @"M:\Folder Directory";
      string filePattern = "*.pdf";
      
      DirectoryInfo di = new DirectoryInfo(dirPath);
      FileInfo[] files = di.GetFiles(filePattern, SearchOption.AllDirectories);
      
      Dictionary<string, FileInfo> matchedFiles = new Dictionary<string, FileInfo>();
      
      foreach (FileInfo file in files)
      {
          string filename = file.Name;
          string[] seperatedFilename = filename.Split('-');
      
          // We are assuming that filenames are consistent
          // As such,
          // the value at seperatedFilename[1] will always be Series No
          // the value at seperatedFilename[2] will always be Revision
          // If this is not the case in every scenario, the following code should expanded to allow other cases
          string seriesNo = seperatedFilename[1];
          string revision = seperatedFilename[2];
      
          if (matchedFiles.ContainsKey(seriesNo))
          {
              FileInfo matchedFile = matchedFiles[seriesNo];
              string matchedRevision = matchedFile.Name.Split('-')[2];
      
              // Compare on the char values - https://docs.microsoft.com/en-us/dotnet/api/system.string.compareordinal?view=netframework-4.7.2
              // If the value is int, then it can be cast to integer for comparison
              if (String.CompareOrdinal(matchedRevision, seriesNo) > 0)
              {
                  // This file is higher than the previous
                  matchedFiles[seriesNo] = file;
              }
          } else
          {
              // Record does not exist - so its is added by default
              matchedFiles.Add(seriesNo, file);
          }
      }
      
      // We have a list of all files which match our criteria
      foreach (FileInfo file in matchedFiles.Values)
      {
          // TODO : Determine if the directory path is also required for the file
          Console.WriteLine(file.FullName);
      }
      

      它将文件名拆分为组成部分,并比较系列名称匹配的修订版;将结果存储在字典中以供以后进一步处理。

      【讨论】:

        【解决方案3】:

        在我看来,这似乎是使用字典的好时机!您可以尝试以下方法:

                String[] files = new string[5];
        
                //group of files with the same series number
                files[0] = "12763-200-A-HelloWorld";
                files[1] = "12763-200-X-HelloWorld";
                files[2] = "12763-200-C-HelloWorld";
        
                //another group of files with the same series number
                files[3] = "12763-203-C-HelloWorld";
                files[4] = "12763-203-Z-HelloWorld";
        
                //all the discting series numbers, since split will the de second position of every string after the '-'
                var distinctSeriesNumbers = files.Select(f => f.Split('-')[1]).Distinct();
                Dictionary<String, List<String>> filesDictionary = new Dictionary<string, List<String>>();
        
                 //for each series number, we will try to get all the files and add them to dictionary
                foreach (var serieNumber in distinctSeriesNumbers)
                {
                    var filesWithSerieNumber = files.Where(f => f.Split('-')[1] == serieNumber).ToList();
                    filesDictionary.Add(serieNumber, filesWithSerieNumber);
                }
        
                List<String> listOfLatestSeries = new List<string>();
        
                //here we will go through de dictionary and get the latest file of each series number
                foreach (KeyValuePair<String, List<String>> entry in filesDictionary)
                {
                    listOfLatestSeries.Add(entry.Value.OrderByDescending(d => d.Split('-')[2]).First());
                }
        
                //now we have the file with the last series number in the list 
                MessageBox.Show(listOfLatestSeries[0]); //result : "12763-200-X-HelloWorld"
                MessageBox.Show(listOfLatestSeries[1]); //result : "12763-203-Z-HelloWorld";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-04
          • 2016-09-08
          • 2017-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多