Search code examples
c#linqfiledirectorydatemodified

Print files modified in the last 24 hours to the console


I'm taking a C# course, and the current assignment is to create a console application that transfers new files (modified in the last 24 hours) from directory "Customer Orders" to directory "Home Office".

At this point I'm just trying to come up with a way to figure out which files are new. To see if it works, I'm using Console.WriteLine to print new files to the console window. However, all it does is print "System.Linq.Enumerable+WhereArrayIterator'1[System.IO.FileInfo]".

I'm incredibly new to this language, and I'm worried I'm already going about everything the wrong way. Here is my code so far (after an hour of googling and getting ideas from StackOverflow):

    class ModifiedFiles
    {
        public string your_dir;

        public IEnumerable<FileInfo> modified()
        {
            your_dir = @"C:\Users\Student\Desktop\Customer Orders";
            var directory = new DirectoryInfo(your_dir);
            DateTime from_date = DateTime.Now.AddDays(-1);
            DateTime to_date = DateTime.Now;
            var files = directory.GetFiles()
              .Where(file => file.LastWriteTime >= from_date && file.LastWriteTime <= to_date);
            return files;
        }
    }
    static void Main(string[] args)
    {
        ModifiedFiles newFiles = new ModifiedFiles();
        Console.WriteLine(newFiles.modified());
        Console.ReadLine();
    }

Can someone kindly point out what is happening here and set me on the right track?


Solution

  • What happens is that every type in C# do inherit the ToString method, which unless overriden will print a default string-representation of the object: its name type.

    Reference:

    https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx

    Now here are 4 examples, printing out every file name, and showing the default behavior of ToString and an overridden behavior:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    namespace MyConsole
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                var path = Environment.CurrentDirectory;
                var fromDate = DateTime.Now.AddDays(-1);
                var toDate = DateTime.Now;
    
                var files = MyClass.GetModifiedFiles(path, fromDate, toDate);
    
                //System.Linq.Enumerable+WhereArrayIterator`1[System.IO.FileInfo]
                Console.WriteLine(files);
    
                //System.Collections.Generic.List`1[System.IO.FileInfo]
                Console.WriteLine(files.ToList());
    
                //MyConsole.exe
                //MyConsole.exe.config
                //MyConsole.pdb
                //MyConsole.vshost.exe
                //MyConsole.vshost.exe.config
                foreach (var file in files)
                {
                    Console.WriteLine(file.Name);
                }
    
                //MyConsole.exe
                //MyConsole.exe.config
                //MyConsole.pdb
                //MyConsole.vshost.exe
                //MyConsole.vshost.exe.config    
                var myClass = new MyClass();
                myClass.FindModifiedFiles(path, fromDate, toDate);
                Console.WriteLine(myClass); // .ToString implicitly called
    
                Console.ReadLine();
            }
        }
    
        internal class MyClass
        {
            private IEnumerable<FileInfo> _modifiedFiles;
    
            public void FindModifiedFiles(string path, DateTime fromDate, DateTime toDate)
            {
                _modifiedFiles = GetModifiedFiles(path, fromDate, toDate);
            }
    
            /* overriding default implemenation of ToString */
    
            /// <summary>Returns a string that represents the current object.</summary>
            /// <returns>A string that represents the current object.</returns>
            /// <filterpriority>2</filterpriority>
            public override string ToString()
            {
                return string.Join(Environment.NewLine, _modifiedFiles.Select(s => s.Name));
            }
    
            public static IEnumerable<FileInfo> GetModifiedFiles(string path, DateTime fromDate, DateTime toDate)
            {
                if (path == null) throw new ArgumentNullException(nameof(path));
                var directory = new DirectoryInfo(path);
                var files = directory.GetFiles()
                    .Where(file => file.LastWriteTime >= fromDate && file.LastWriteTime <= toDate);
                return files;
            }
        }
    }