【问题标题】:Naming a file based on other files in the folder?根据文件夹中的其他文件命名文件?
【发布时间】:2013-09-07 22:12:54
【问题描述】:

我有一个包含这样命名的文本文件的文件夹,例如:0、1、2、3...

我需要检查文件名中的最大数字。

例如,如果我有文件 1.txt 和 2.txt 和 3.txt,我想得到 3。

我该怎么做?

谢谢,

【问题讨论】:

  • 它是什么?基于 0 的计数还是基于 1 的计数?
  • 它从一开始,但与它从什么数字开始无关。重要的是最高的数字。
  • 一致性是开发中一个非常重要的属性,所以提出一个一致的问题是一个非常好的开始。

标签: c# file filenames


【解决方案1】:

一些 LINQ 的优点:

var maxNumber = Directory.GetFiles(@"C:\test")
                         .Select(file => Path.GetFileNameWithoutExtension(file))
                         .Where(filename => filename.All(ch => char.IsNumber(ch)))
                         .Select(filename => int.Parse(filename))
                         .Max();

【讨论】:

    【解决方案2】:

    试试这样的

    private static void CreateNewFile()
           {
              string[] files = Directory.GetFiles(@"c:\test");
              int maxNumb = 0;
              foreach (var item in files)
              {
                  FileInfo file = new FileInfo(item);
                  maxNumb = Math.Max(maxNumb,     int.Parse(Path.GetFileNameWithoutExtension(file.FullName)));
              }
            File.Create(string.Format("{0}.txt", maxNumb++));
           }
    

    希望对你有所帮助

    【讨论】:

      【解决方案3】:

      听起来像是作业,但时间不早了,我心情很好,所以:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.IO;
      
      namespace Test
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //set the path to check:
                  var path = @"D:\Thefiles";
                  //create a dictionary to store the results so we can also remember the file name
                  var results = new Dictionary<int, string>();
                  //loop all the files
                  foreach (var file in Directory.GetFiles(path))
                  {
                      //get the name without any extension
                      var namePart = Path.GetFileNameWithoutExtension(file);
                      //try to cast it as an integer; if this fails then we can't count it so ignore it
                      int fileNumber = 0;
                      if (Int32.TryParse(namePart, out fileNumber))
                      {
                          //add to dictionary
                          results.Add(fileNumber, file);
                      }
                  }
                  //only show the largest file if we actually have a result
                  if (results.Any())
                  {
                      var largestFileName = results.OrderByDescending(r => r.Key).First();
                      Console.WriteLine("Largest name is {0} and the file name is {1}", largestFileName.Key, largestFileName.Value);
                  }
                  else
                  {
                      Console.WriteLine("No valid results!");
                  }
                  Console.ReadLine();
              }
          }
      }
      

      【讨论】:

      • @Marco,很公平,在我的辩护中,我这样做已经相当晚了:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 2019-07-19
      相关资源
      最近更新 更多