使用foreach将一个数组的名称存储在另一个数组中的c#中[英] using foreach to store names of one array in another in c#

本文是小编为大家收集整理的关于使用foreach将一个数组的名称存储在另一个数组中的c#中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

现在,我有一个带有名称格式的目录,例如" Ext2-401-B-140422-1540-1542.MP4",其中" 140422"部分表示日期.现在假设这串文件具有140421、140422、140423的日期(对于每个日期,都有几个文件).现在,我将根据它们的日期对这些文件进行排序,因此我想知道如何获得这些名称(140421,140422等).我尝试这样的尝试:

directory = new DirectoryInfo(camera_dir);    
string[] date = new string[directory.GetFiles().Length];
foreach (FileInfo file in directory.GetFiles())
{ 
    foreach(string name in date)
    {
        name = file.Name.Substring(11, 6);
    }
}

错误消息是我无法分配给名称.所以有人可以帮忙吗?

推荐答案

directory = new DirectoryInfo(camera_dir);    
string[] date = new string[directory.GetFiles().Length];
int i=0;
foreach (FileInfo file in directory.GetFiles())
{ 
    name[i] = file.Name.Substring(11, 6);
    i++;
}

其他推荐答案

您可以使用Linq简化一点.

要仅获取文件名,您只需要将每个FileInfo投射到string中:

var dates = directory
    .EnumerateFiles("*.mp4")
    .Select(f => f.Name)
    .ToArray();

要获取一个订购列表,您还需要使用OrderBy并指定要订购的值:

var dates = directory
    .EnumerateFiles("*.mp4")
    .Select(f => f.Name)
    .OrderBy(f => f.Substring(11, 6)) // this will throw if string is too short
    .ToArray();

当文件名未正确格式时,您还应该添加一些验证以防止例外.您至少可以做的就是检查字符串是否足够长,可以提取这6个字符:

var dates = directory
    .EnumerateFiles("*.mp4")
    .Select(f => f.Name)
    .Where(f => f.Length >= 17) // check if there are enough characters
    .OrderBy(f => f.Substring(11, 6))
    .ToArray();

其他推荐答案

尝试以下方法:

    var files = directory.GetFiles();
    string[] dates = new string[files.Length];
    for(int i = 0; i < files.Length; i++)
    { 
        dates[i] = files[i].Name.Substring(11, 6);
    }

本文地址:https://www.itbaoku.cn/post/2352683.html

问题描述

Now I have a directory with bunch files with format of name like "EXT2-401-B-140422-1540-1542.mp4", within which the "140422" part indicates the date. Now assume that this bunch of files have the dates like 140421, 140422, 140423...(for every date there are couple of files). Now I shall sort these files according to their dates, so I'd like to know how could I get these names (140421,140422,etc). I tried like this:

directory = new DirectoryInfo(camera_dir);    
string[] date = new string[directory.GetFiles().Length];
foreach (FileInfo file in directory.GetFiles())
{ 
    foreach(string name in date)
    {
        name = file.Name.Substring(11, 6);
    }
}

And the error message is that I can't assign to name. So anybody could help?

推荐答案

directory = new DirectoryInfo(camera_dir);    
string[] date = new string[directory.GetFiles().Length];
int i=0;
foreach (FileInfo file in directory.GetFiles())
{ 
    name[i] = file.Name.Substring(11, 6);
    i++;
}

其他推荐答案

You can use LINQ to simplify this a bit.

To get filenames only, you simply need to project each FileInfo into a string:

var dates = directory
    .EnumerateFiles("*.mp4")
    .Select(f => f.Name)
    .ToArray();

To get an ordered list, you also need to use OrderBy and specify the value to be used for ordering:

var dates = directory
    .EnumerateFiles("*.mp4")
    .Select(f => f.Name)
    .OrderBy(f => f.Substring(11, 6)) // this will throw if string is too short
    .ToArray();

You should also probably add some validation to prevent exceptions when filenames are not formatted properly. The least you can do is check if the string is long enough to have these 6 characters extracted:

var dates = directory
    .EnumerateFiles("*.mp4")
    .Select(f => f.Name)
    .Where(f => f.Length >= 17) // check if there are enough characters
    .OrderBy(f => f.Substring(11, 6))
    .ToArray();

其他推荐答案

try this:

    var files = directory.GetFiles();
    string[] dates = new string[files.Length];
    for(int i = 0; i < files.Length; i++)
    { 
        dates[i] = files[i].Name.Substring(11, 6);
    }