C# 找到日期y(可能我们有1000个文件999个有日期x,1个有日期y)和相应的文件?
请建议执行以下逻辑的最简单方法。 我们可以使用数据表并从表中查询?C# 找到日期y(可能我们有1000个文件999个有日期x,1个有日期y)和相应的文件?,c#,dictionary,C#,Dictionary,请建议执行以下逻辑的最简单方法。 我们可以使用数据表并从表中查询? string[] filePath = Directory.GetFiles(directory, "*.txt",SearchOption.AllDirectories); Dictionary<string, DateTime> dic = new Dictionary<string, DateTime>(); foreach (string CurrentPath in filePath) {
string[] filePath = Directory.GetFiles(directory, "*.txt",SearchOption.AllDirectories);
Dictionary<string, DateTime> dic = new Dictionary<string, DateTime>();
foreach (string CurrentPath in filePath)
{
string Content = System.IO.File.ReadAllText(@CurrentPath);
string loadedDate = DateTime.ParseExact(Content.Substring(9,8), "yyyyMMdd",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
DateTime Datevalue = DateTime.Parse(loadedDate);
String FileName = Path.GetFileName(CurrentPath);
dic.Add(Path.GetFileName(CurrentPath), Datevalue);
}
string[]filePath=Directory.GetFiles(目录“*.txt”、SearchOption.AllDirectories);
Dictionary dic=新字典();
foreach(文件路径中的字符串CurrentPath)
{
字符串内容=System.IO.File.ReadAllText(@CurrentPath);
string loadedDate=DateTime.ParseExact(Content.Substring(9,8),“yyyyymmdd”,
CultureInfo.InvariantCulture).ToString(“yyyy/MM/dd”);
DateTime Datevalue=DateTime.Parse(loadedDate);
字符串文件名=Path.GetFileName(CurrentPath);
dic.Add(Path.GetFileName(CurrentPath),Datevalue);
}
说明:
filepath包含.txt文件路径的列表
在foreach循环中,读取所有文件内容并找到内容中存在的日期
在字典中添加了所有日期值和文件名
现在我需要找到不同的日期和相应的文件名。
例如:我们有3个文件File1.txt、File2.txt、File3.txt。日期值为2019年9月25日的前两个文件
第三个文件的日期值为2019年9月24日。
在这里,我们需要找到与其他日期不同的日期,即2019年9月24日和相应的日期
文件名File3.txt
如何从字典值中找到日期(与其他日期不同的日期)和相应的文件名
请建议执行此逻辑的最简单方法或任何其他方法。您可以使用字典获取文本文件的键值对以及从中提取的日期
string directory = @"C:\";
string[] filePath = Directory.GetFiles(directory, "*.txt", SearchOption.AllDirectories);
Dictionary<string, DateTime> dic = new Dictionary<string, DateTime>();
foreach (string CurrentPath in filePath)
{
string Content = File.ReadAllText(@CurrentPath);
string loadedDate = DateTime.ParseExact(Content.Substring(9, 8), "yyyyMMdd",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
DateTime Datevalue = DateTime.Parse(loadedDate);
dic.Add(Path.GetFileName(CurrentPath), Datevalue);
}
希望对您有所帮助。您可以使用包含每个日期出现次数的帮助词典。返回文件名的方法如下:
public string GetFileName()
{
Dictionary<DateTime, int> dts = new Dictionary<DateTime, int>();
foreach (KeyValuePair<string, DateTime> item in dic)
{
if (dts.ContainsKey(item.Value))
{
dts[item.Value]++;
}
else
{
dts.Add(item.Value, 1);
}
}
var value = dts.Where(x => x.Value == 1).Select(x => x.Key).FirstOrDefault();
foreach (KeyValuePair<string, DateTime> item in dic)
{
if (item.Value == value)
{
return item.Key;
}
}
return "";
}
公共字符串GetFileName()
{
Dictionary dts=新字典();
foreach(dic中的KeyValuePair项)
{
if(dts.ContainsKey(项目价值))
{
dts[项目值]+;
}
其他的
{
dts.Add(第1项价值);
}
}
var value=dts.Where(x=>x.value==1);
foreach(dic中的KeyValuePair项)
{
如果(item.Value==值)
{
返回项。键;
}
}
返回“”;
}
您的要求不明确,因为无法确定哪一天是正确的日期。但是,如果要查找日期列表以及与这些日期关联的文件,可以通过将日期作为键来改进代码
因此,您的字典声明可以更改为-
Dictionary<DateTime, List<string>> dic = new Dictionary<DateTime, List<string>>();
现在您有了唯一的日期和相关的文件列表来执行下一步…谢谢JQSOFT。当然,我可以使用字典来获得键值对。但是如何从字典中找到与其他日期不同的日期和相应的文件如何从字典值中找到与其他日期不同的日期和相应的文件。哪一个文件与“其他日期”不同?如果在这10个文件中,2个文件的日期为X,3个文件的日期为Y,2个文件的日期为Z,而其余文件的日期则不同呢?嗨,詹姆斯,最大可能性是2。如果我们有1000个文件,可能999个文件的日期为x,其余1个文件的日期为y。您可以使用LINQ
GroupBy
按键值对的值分量对键值对进行分组。现在您可以查看组计数。最大可能性为2。如果我们有1000个文件,可能999个文件的日期是x,剩下的1个文件的日期是y。好的,那么您可以迭代键,获得不同的日期和相关文件的列表。
public string GetFileName()
{
Dictionary<DateTime, int> dts = new Dictionary<DateTime, int>();
foreach (KeyValuePair<string, DateTime> item in dic)
{
if (dts.ContainsKey(item.Value))
{
dts[item.Value]++;
}
else
{
dts.Add(item.Value, 1);
}
}
var value = dts.Where(x => x.Value == 1).Select(x => x.Key).FirstOrDefault();
foreach (KeyValuePair<string, DateTime> item in dic)
{
if (item.Value == value)
{
return item.Key;
}
}
return "";
}
Dictionary<DateTime, List<string>> dic = new Dictionary<DateTime, List<string>>();
AddFileName(dic,Path.GetFileName(CurrentPath), Datevalue);
void AddFileName(Dictionary<DateTime, List<string>> fileInfo, DateTime dt, string fileName)
{
if (fileInfo.ContainsKey(dt))
{
var fileList = fileInfo[dt];
fileList.Add(fileName);
}
else
{
fileInfo.Add(dt, new List<string> { fileName });
}
}