问题描述
在下面的代码中,字符串数组fileNames填充有目录中的文件.
该目录每天都有文件.即FileAxxx.csv,其中xxx表示YYYYmmdd
问题:
如何仅用今天的文件填充此字符串数组?
谢谢
String [] fileNames;
//填充fileNames字符串数组...
ArrayList fileNamesArray = new ArrayList(fileNames);
Hi,
In the code below the string array fileNames is populated with files from a directory.
The directory has files for each day. i.e. FileAxxx.csv where xxx refers to YYYYmmdd
Question:
How can this string array be populated only with files for today''s date?
Thanks
String[] fileNames;
//populate fileNames string array...
ArrayList fileNamesArray = new ArrayList(fileNames);
推荐答案
var query = from file in fileNames
where (new FileInfo(file)).LastWriteTime.Date == DateTime.Now.Date
select file;
fileNames = query.ToArray<string>();
谢谢您的答复,但我忘记说出于项目原因我不想使用LINQ."
然后尝试:
"Hi, Thanks for the reply But I forgot to say that I do not want to use LINQ for project reasons."
Then try:
List<string> todayFiles = new List<string>();
foreach (string file in files)
{
if ((new FileInfo(file)).LastWriteTime.Date == DateTime.Now.Date)
{
todayFiles.Add(file);
}
}
files = todayFiles.ToArray<string>();
string[] fileNames = {"abcdef20120216.csv","ijklmn20120215.csv","pqrst20120216.csv"};
DateTime today = DateTime.Now;
string searchDate =
string.Format("{0:0000}{1:00}{2:00}.csv",today.Year,today.Month,today.Day);
ArrayList fileNamesArray = new ArrayList();
foreach(string fileName in fileNames){
if (fileName.EndsWith(searchDate))
fileNamesArray.Add(fileName);
}
如果您的问题得到解决,那么您可以接受并投票解决该问题,否则请发布您的查询
PES
If your problem is solved then you may accept and vote the solution, otherwise please post your queries
PES
string[] name = Directory.GetFiles("<path>", "*" + DateTime.Now.ToString("yyyyMMdd") + ".csv");</path>
这篇关于填充字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!