У меня есть папка с множеством таких файлов:
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;
}
}