Question

I am having a problem with my c# code I can't seem to get my if statement to work I think it is trying to reference the wrong part of code for some reason. I have checked that I have all the right references in and all the right uses in. I have pasted the offending code bellow:

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);
        }
    }

any help that could be given would be gratefully appreciated thanks.

Was it helpful?

Solution

What you want to do is probably this?

        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);
            }
        }

OTHER TIPS

Well, fi is an array so fi.LastWriteTime.Date == DateTime.Today.Date would give that error. Correctly.

You don't seem to use the returned directories for anything else, so I'm not able to suggest a 'fix'.

You are calling LastWriteTime on the array, and an array doesn't have this property.

You need to call LastWriteTime on the members of the array, e.g.

fi[0].LastWriteTime

Or to iterate over all the files:

foreach(var file in fi) 
{
   if(file.LastWriteTime.Date == DateTime.Today.Date)
   {
       ....
   }
}

Error is simple you are using LastWriteTime on array instead of the FileInfo item. You should use an index in the code like this:

fi[0].LastWriteTime.Date  ///your code

replace 0 with your index num or use it in a foreach loop like this:

foreach(var item in fi)
{
            if (item.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);
            }
}

As previously stated, this Property is invalid on an array. Just enclose your FileInfo with a foreach loop:

foreach(FileInfo fi in 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);
   }
 }

You need

foreach(FileInfo fi in 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);
    }
}

You are missinf a for loop:

 for (int i = 0; i < fi.Length; ++i)
 {
     if (fi[i].LastWriteTime.Date == DateTime.Today.Date)
     ...
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top