我的C#代码有问题,我似乎无法使我的if语句正常工作,我认为由于某种原因它试图引用错误的代码部分。我检查了是否具有所有正确的引用以及所有正确的用途。我粘贴了下面的令人讨厌的代码:

FolderBrowserDialog dlg2 = new FolderBrowserDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
//do whatever with dlg.SelectedPath
{

    string searchPattern = "*";
    DirectoryInfo source = new DirectoryInfo(dlg.SelectedPath);
    DirectoryInfo target = new DirectoryInfo(dlg2.SelectedPath);

    DirectoryInfo dir = new DirectoryInfo(dlg.SelectedPath);
    FileInfo[] fi = dir.GetFiles("*", SearchOption.AllDirectories);
    {
        if (fi.LastWriteTime.Date == DateTime.Today.Date)
        {
            FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
            for (int i = 0; i < sourceFiles.Length; ++i)
                File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
        }
    }

感谢您提供的任何帮助。

最佳答案

您想做的可能是这个吗?

        FileInfo[] fis = dir.GetFiles("*", SearchOption.AllDirectories);
        foreach (FileInfo fi in fis)
        {
            if (fi.LastWriteTime.Date == DateTime.Today.Date)
            {
                FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
                for (int i = 0; i < sourceFiles.Length; ++i)
                    File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
            }
        }

10-08 12:46