我有这段C#代码,我试图有条件地运行它的一部分:
private void GetMyFiles()
{
string rootDirectory = @"" + txtPathToScan.Text + "";
string extension = "*";
List<FileInfo> files = GetFilesRecursively(rootDirectory, extension);
SqlCeConnection conParam = new SqlCeConnection();
conParam.ConnectionString = FileFinder.Properties.Settings.Default.Database1ConnectionString;
conParam.Open();
SqlCeCommand DBParams = new SqlCeCommand("SELECT FileSize, DateLastAccessedGT, DateLastAccessedLT, DateLastModifiedGT, DateLastModifiedLT FROM FF_tblParams", conParam);
SqlCeDataReader reader;
reader = DBParams.ExecuteReader();
reader.Read();
using (SqlCeDataReader DT1 = DBParams.ExecuteReader())
{
double FLenP = Convert.ToDouble(reader["FileSize"]);
DateTime FAccessGT = Convert.ToDateTime(reader["DateLastAccessedGT"]);
DateTime FAccessLT = Convert.ToDateTime(reader["DateLastAccessedLT"]);
DateTime FModifiedGT = Convert.ToDateTime(reader["DateLastModifiedGT"]);
DateTime FModifiedLT = Convert.ToDateTime(reader["DateLastModifiedLT"]);
//Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
foreach (FileInfo file in files)
{
//Console.WriteLine(file);
var info = new System.IO.FileInfo(@"" + file + "");
string FName = info.Name;
string FPath = file.FullName;
double FLen = (info.Length + 1023) / 1024;
DateTime FCreate = info.CreationTime;
DateTime FAccess = info.LastAccessTime;
DateTime FWrite = info.LastWriteTime;
//**This is where my problem is**
if (FLenP > FLen && FAccessLT <= FAccess <= FAccessGT && FModifiedLT <= FWrite <= FModifiedGT)
{
AddNewRecords(FPath, FName, FLen, FCreate, FAccess, FWrite);
}
}
}
}
我首先通过注释 If 语句来确保它看起来没问题。当我运行代码时,我休息一下,看看发生了什么。在日期看起来像日期等等一切看起来都不错......但是,当我取消注释 If 语句时,我收到此错误:
运算符'&lt; ='不能应用于'bool'类型的操作数 '的System.DateTime'
知道为什么会这样吗?
答案 0 :(得分:2)
这并不是按照你的想法解释的:
FLenP > FLen && FAccessLT <= FAccess <= FAccessGT && FModifiedLT <= FWrite <= FModifiedGT
按顺序评估操作员,他们不会组合在一起。所以这个:
FAccessLT <= FAccess <= FAccessGT
成为这个:
(FAccessLT <= FAccess) <= FAccessGT
括号内的内容评估为bool
,然后无法在<=
表达式中使用DateTime
。因此错误。
一次只能进行一次比较。您需要分离逻辑条件。在上面的部分,它可能看起来像这样:
FAccessLT <= FAccess && FAccess <= FAccessGT
答案 1 :(得分:0)
你无法比较3个操作数。当你这样做时,它评估bool vs第三个操作数。说
if (1 < 2 < 3) { ... }
这等于
if ( true < 3) { ... }