C# 获取文件时的分析错误取决于文件创建时间

C# 获取文件时的分析错误取决于文件创建时间,c#,.net,winforms,file,C#,.net,Winforms,File,我有这样的路径…“C:\restore\restoredb\” 在那条路径中,我有这样的文件 backup-2011-10-12T17-16-51.zip backup-2011-10-11T13-24-45.zip 我有一个表单,在这个表单中我有一个列表框和组合框(cbrestore),我有这样的组合框项目…月,3个月,6个月,年 我想要的是,如果我选择组合框项目(月),我想使用文件创建时间(2011年10月12日至2011年9月12日)显示存储在该文件夹中的文件名,如下图所示文件.Ge

我有这样的路径…“C:\restore\restoredb\”

在那条路径中,我有这样的文件

 backup-2011-10-12T17-16-51.zip
 backup-2011-10-11T13-24-45.zip
我有一个表单,在这个表单中我有一个列表框和组合框(cbrestore),我有这样的组合框项目…月,3个月,6个月,年

我想要的是,如果我选择组合框项目(月),我想使用文件创建时间(2011年10月12日至2011年9月12日)显示存储在该文件夹中的文件名,如下图所示<代码>文件.Getcreationtime

如果我选择组合框项目(3个月),我希望在列表框中显示这些日期(2011年10月12日至2011年7月12日)之间存储在该文件夹中的文件名

为此,我试过这样做

 List<String> t = Directory.GetFiles(@"C:\restore\restoredb\").ToList();
List<String> y = new List<string>();
List<String> u = new List<string>();



foreach (var zzz in t)
{
    y.Add(Path.GetFileName(zzz));
}


if (comboBox1.Text == "Month")
{
    u =
   (from String s in y where ((DateTime.Now.Month - (DateTime.Parse(File.GetCreationTime(s)) < 1) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0) select s).
       ToList();
}
List t=Directory.GetFiles(@“C:\restore\restoredb\”).ToList();
列表y=新列表();
列表u=新列表();
foreach(t中的变量zzz)
{
y、 添加(Path.GetFileName(zzz));
}
如果(comboBox1.Text==“月”)
{
u=
(来自y中的字符串s,其中((DateTime.Now.Month-(DateTime.Parse(File.GetCreationTime))<1)和&(DateTime.Now.Year-DateTime.Parse(s.Substring(8,10)).Year==0)选择s)。
托利斯特();
}
但我这样说是有错误的

“system.datatime.parse(string)的最佳重载方法匹配有一些无效参数….类似于此,我在这一行
(DateTime.parse(File.GetCreationTime))

有人能帮我吗。。。。。 非常感谢…

参数类型为
String
并返回
DateTime
,因此您尝试将DateTime值作为字符串类型参数传递

所以只需改变如下条件:

where ((DateTime.Now.Month - File.GetCreationTime(s).Month) < 1) 
       && 
       (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0)
EDIT2:

string path = @"C:\restore\restoredb\";            
IList<String> allFiles = Directory.GetFiles(path).ToList();
IList<String> fileNames = new List<string>();
IList<String> filesCreatedInThisMonth = new List<string>();
fileNames = allFiles.Select(filePath => Path.GetFileName(filePath)).ToList();

if (comboBox1.Text == "Month")
{
    filesCreatedInThisMonth =
        allFiles.Where(fileName =>
                {
                    return File.GetCreationTime(fileName).Month
                            == (DateTime.Now.Month == 1 ? 12 : DateTime.Now.Month - 1)
                            &&
                            (DateTime.Now.Year == DateTime.Parse(fileName.Substring(8, 10)).Year);
                }).ToList();
 }
stringpath=@“C:\restore\restoredb\”;
IList allFiles=Directory.GetFiles(path.ToList();
IList fileNames=新列表();
IList filesCreatedInThisMonth=新列表();
fileNames=allFiles.Select(filePath=>Path.GetFileName(filePath)).ToList();
如果(comboBox1.Text==“月”)
{
本月创建的文件=
所有文件。其中(文件名=>
{
返回文件.GetCreationTime(文件名).Month
==(DateTime.Now.Month==1?12:DateTime.Now.Month-1)
&&
(DateTime.Now.Year==DateTime.Parse(fileName.Substring(8,10)).Year);
}).ToList();
}
EDIT3:

IList<String> filesCreatedInThisMonth = new List<string>();
IList<String> fileNames = new List<string>();
// Key - Full file path
// Value - File creation DateTime extracted from the file name
IDictionary<string, DateTime> filePathToDateMap =
    Directory.GetFiles(path).ToDictionary(
        filePath => filePath,
        filePath => DateTime.Parse(Path.GetFileName(filePath).Substring(8, 10)));

// mapEntry - KeyValuePair
// Key - filePath, Value - creation DateTime extracted from the file name
filesCreatedInThisMonth =
        filePathToDateMap.Where(mapEntry =>
                {
                    return File.GetCreationTime(mapEntry.Key).Month
                            == (DateTime.Now.Month == 1 ? 12 : DateTime.Now.Month - 1)
                            &&
                            (DateTime.Now.Year == mapEntry.Value.Year);
                }).Select(entry => entry.Key)
                .ToList();           
IList filesCreatedInThisMonth=新列表();
IList fileNames=新列表();
//密钥-完整文件路径
//Value-从文件名中提取的文件创建日期时间
IDictionary filePathToDateMap=
Directory.GetFiles(path).ToDictionary(
filePath=>filePath,
filePath=>DateTime.Parse(Path.GetFileName(filePath.Substring(8,10));
//mapEntry-KeyValuePair
//Key-filePath,从文件名中提取的值创建日期时间
本月创建的文件=
filePathToDateMap.Where(mapEntry=>
{
返回文件.GetCreationTime(mapEntry.Key).Month
==(DateTime.Now.Month==1?12:DateTime.Now.Month-1)
&&
(DateTime.Now.Year==mapEntry.Value.Year);
}).Select(entry=>entry.Key)
.ToList();
或者,当文件创建时间不是从文件名而是从实际创建时间检索时:

IDictionary<string, DateTime> filePathToDateMap =
Directory.GetFiles(path).ToDictionary(
    filePath => filePath,
    filePath => File.GetCreationTime(filePath));
IDictionary filePathToDateMap=
Directory.GetFiles(path).ToDictionary(
filePath=>filePath,
filePath=>File.GetCreationTime(filePath));
参数类型为
String
并返回
DateTime
,因此您试图将DateTime值作为字符串类型参数传递

所以只需改变如下条件:

where ((DateTime.Now.Month - File.GetCreationTime(s).Month) < 1) 
       && 
       (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0)
EDIT2:

string path = @"C:\restore\restoredb\";            
IList<String> allFiles = Directory.GetFiles(path).ToList();
IList<String> fileNames = new List<string>();
IList<String> filesCreatedInThisMonth = new List<string>();
fileNames = allFiles.Select(filePath => Path.GetFileName(filePath)).ToList();

if (comboBox1.Text == "Month")
{
    filesCreatedInThisMonth =
        allFiles.Where(fileName =>
                {
                    return File.GetCreationTime(fileName).Month
                            == (DateTime.Now.Month == 1 ? 12 : DateTime.Now.Month - 1)
                            &&
                            (DateTime.Now.Year == DateTime.Parse(fileName.Substring(8, 10)).Year);
                }).ToList();
 }
stringpath=@“C:\restore\restoredb\”;
IList allFiles=Directory.GetFiles(path.ToList();
IList fileNames=新列表();
IList filesCreatedInThisMonth=新列表();
fileNames=allFiles.Select(filePath=>Path.GetFileName(filePath)).ToList();
如果(comboBox1.Text==“月”)
{
本月创建的文件=
所有文件。其中(文件名=>
{
返回文件.GetCreationTime(文件名).Month
==(DateTime.Now.Month==1?12:DateTime.Now.Month-1)
&&
(DateTime.Now.Year==DateTime.Parse(fileName.Substring(8,10)).Year);
}).ToList();
}
EDIT3:

IList<String> filesCreatedInThisMonth = new List<string>();
IList<String> fileNames = new List<string>();
// Key - Full file path
// Value - File creation DateTime extracted from the file name
IDictionary<string, DateTime> filePathToDateMap =
    Directory.GetFiles(path).ToDictionary(
        filePath => filePath,
        filePath => DateTime.Parse(Path.GetFileName(filePath).Substring(8, 10)));

// mapEntry - KeyValuePair
// Key - filePath, Value - creation DateTime extracted from the file name
filesCreatedInThisMonth =
        filePathToDateMap.Where(mapEntry =>
                {
                    return File.GetCreationTime(mapEntry.Key).Month
                            == (DateTime.Now.Month == 1 ? 12 : DateTime.Now.Month - 1)
                            &&
                            (DateTime.Now.Year == mapEntry.Value.Year);
                }).Select(entry => entry.Key)
                .ToList();           
IList filesCreatedInThisMonth=新列表();
IList fileNames=新列表();
//密钥-完整文件路径
//Value-从文件名中提取的文件创建日期时间
IDictionary filePathToDateMap=
Directory.GetFiles(path).ToDictionary(
filePath=>filePath,
filePath=>DateTime.Parse(Path.GetFileName(filePath.Substring(8,10));
//mapEntry-KeyValuePair
//Key-filePath,从文件名中提取的值创建日期时间
本月创建的文件=
filePathToDateMap.Where(mapEntry=>
{
返回文件.GetCreationTime(mapEntry.Key).Month
==(DateTime.Now.Month==1?12:DateTime.Now.Month-1)
&&
(DateTime.Now.Year==mapEntry.Value.Year);
}).Select(entry=>entry.Key)
.ToList();
或者,当文件创建时间不是从文件名而是从实际创建时间检索时:

IDictionary<string, DateTime> filePathToDateMap =
Directory.GetFiles(path).ToDictionary(
    filePath => filePath,
    filePath => File.GetCreationTime(filePath));
IDictionary filePathToDateMap=
Directory.GetFiles(path).ToDictionary(
filePath=>filePath,
filePath=>File.GetCreationTime(filePath));
您的错误如下:

(DateTime.Parse(File.GetCreationTime(s))
GetCreationTime返回的是日期时间,而不是字符串(DateTime.Parse期望的类型…)

您应该替换File.GetCreationTime的以前代码,而不使用DateTime.Parse

您的错误如下:

(DateTime.Parse(File.GetCreationTime(s))