Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据文件夹中的其他文件命名文件?_C#_File_Filenames - Fatal编程技术网

C# 根据文件夹中的其他文件命名文件?

C# 根据文件夹中的其他文件命名文件?,c#,file,filenames,C#,File,Filenames,我有一个文件夹,其中包含如下命名的文本文件,例如:0、1、2、3 我需要检查文件名中的最大数字 例如,如果我有文件1.txt、2.txt和3.txt,我想得到3 我怎么能这么做 谢谢,试试这样的 private static void CreateNewFile() { string[] files = Directory.GetFiles(@"c:\test"); int maxNumb = 0; foreach (va

我有一个文件夹,其中包含如下命名的文本文件,例如:0、1、2、3

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

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

我怎么能这么做


谢谢,

试试这样的

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++));
       }

希望这个帮助听起来像是家庭作业,但是已经很晚了,我心情很好,所以:

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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
名称空间测试
{
班级计划
{
静态void Main(字符串[]参数)
{
//设置要检查的路径:
var path=@“D:\Thefiles”;
//创建一个字典来存储结果,这样我们还可以记住文件名
var results=newdictionary();
//循环所有文件
foreach(Directory.GetFiles(path)中的var文件)
{
//获取不带任何扩展名的名称
var namePart=Path.GetFileNameWithoutExtension(文件);
//尝试将其转换为整数;如果失败,则无法计算它,因此忽略它
int fileNumber=0;
if(Int32.TryParse(namePart,out fileNumber))
{
//添加到字典
结果.添加(文件号、文件);
}
}
//仅当我们实际有结果时才显示最大的文件
if(results.Any())
{
var largestFileName=results.OrderByDescending(r=>r.Key).First();
WriteLine(“最大名称为{0},文件名为{1}”,largestFileName.Key,largestFileName.Value);
}
其他的
{
WriteLine(“无有效结果!”);
}
Console.ReadLine();
}
}
}
一些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();

是哪一个?以0为基础还是以1为基础计数?它从1开始,但它不知道以什么数字开始。最重要的是最高的数字。一致性是开发中非常重要的属性,因此提出一致性问题是一个非常好的开始。@Marco,很公平,在我的辩护中,我这么做的时候已经很晚了:)