我正在编写一个表单应用程序,其中它显示Datagridview中目录中的所有PDF文件。 现在,文件名格式通常为 12740-250-B-文件名(因此基本上是XXXXX-XXX-X-XXXXXXX)。 因此,第一个数字是项目编号,破折号后的第二个数字是序列号-字母是文件的修订版。
我想按一个按钮时,它将找到具有相同序列号的文件(XXXXX系列编号-修订版-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电子表格中。
答案 0 :(得分:0)
假设您的集合是一个文件名列表,这是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”
答案 1 :(得分:0)
您可以尝试以下操作:
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);
}
它将文件名拆分为各个部分,并比较系列名称匹配的修订版本;将结果存储在字典中,以便以后进行进一步处理。
答案 2 :(得分:0)
在我看来,这本来是一本好书,适合使用字典!您可以尝试以下操作:
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";
答案 3 :(得分:-1)
您可以通过以下步骤实现。
相对于“-”分割文件名
string [] splittedFileName = fileName.Split('-');
当文件名为 12763-200-A-HelloWorld 时,它给出4个值,分别为 12763、200,A,HelloWorld 。
在那儿,索引2处的值为您的修订号。即 splittedFileName [2] 。
使用 TryParse 如下获得等效的整数值。
int值= Int32.TryParse(splittedFileName [2]);
现在比较int值并找出更大的值以获取最新版本。