c# - Print files modified in the last 24 hours to the console -
i'm taking c# course, , current assignment create console application transfers new files (modified in last 24 hours) directory "customer orders" directory "home office".
at point i'm trying come way figure out files new. see if works, i'm using console.writeline print new files console window. however, print "system.linq.enumerable+wherearrayiterator'1[system.io.fileinfo]".
i'm incredibly new language, , i'm worried i'm going wrong way. here code far (after hour of googling , getting ideas 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 kindly point out happening here , set me on right track?
what happens every type in c# inherit tostring
method, unless overriden print default string-representation of object: its name type.
reference:
https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx
now here 4 examples, printing out every file name, , showing default behavior of tostring
, 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 string represents current object.</summary> /// <returns>a string represents 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; } } }
Comments
Post a Comment