home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2738
Discussions: 186,607, Posts: 2,227,060, Members: 230,243
Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
Old June 29th, 2008, 12:48 AM   Digg it!   #1 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 20,458
C# + LINQ Help

ok i know I can do this with a List.Sort or with a crappy bubble sort loop.. but thats not nearly enough fun

So I'm trying to use LINQ which is supposed to be great for this stuff

So ultimate goal is to connect to a directory of a lot of files and get the oldest .log file w/o doing any loops.

So far I have

Code:
namespace PollExchangeProperties
{
    class Program
    {
        static void Main(string[] args)
        {

            DirectoryInfo di = new DirectoryInfo(@"C:\code\");
            foreach (DirectoryInfo d in di.GetDirectories())
            {
                Console.WriteLine("Directory: {0}", d.Name);
                Console.WriteLine("# Files: {0}", d.GetFiles("*").Length );
                foreach (FileInfo f in d.GetFiles())
                {
                    Console.WriteLine("FileName: {0}\\{1} = {2} kb", d.Name, f.Name, f.Length/1024);
                }
            }
       }
  }
}
This obviously uses a loop, but this is just for testing

So basically I want to do this

GetDirectory
Get all *.log files in directory and return date of oldest file in that directory

Yes I know it can be done with bat easily, but again thats not a challenge
This is a great excercise for me to learn C#

vass0922 is online now   Reply With Quote
TechIMO.com Ads - Login or register for less ads.
How many errors does your computer have?

You no longer need to guess! This free stability scan and registry cleaner download will give you a complete diagnosis of your Windows registry, identifying errors and conflicts.

FREE instant scan


Guest, Register Free! to remove this ad and get your tech support questions answered in minutes!
Old June 29th, 2008, 01:37 AM     #2 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 20,458
if this were written in Ruby, this is exactly what I want it to do

Code:
def oldest_file(dir) 
  Dir.entries(dir).map { |e| 
    File.join(dir, e) 
  }.select { |f| 
    File.file? f 
  }.sort_by { |f| 
    File.mtime f 
  }.first 
end

vass0922 is online now   Reply With Quote
Old June 29th, 2008, 01:59 AM     #3 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 20,458

vass0922 is online now   Reply With Quote
Old June 29th, 2008, 09:19 AM     #4 (permalink)
Junior Member
 
Join Date: Jun 2008
Posts: 1
Post
How to Use LINQ to Filter a List of Files by Date

LINQ Exchange has a good article on sorting and filtering a list of files by date.
How to Use LINQ to Filter a List of Files by Date
merlin981 is offline   Reply With Quote
Old June 29th, 2008, 12:39 PM     #5 (permalink)
Ultimate Member
 
Join Date: Oct 2001
Posts: 20,458
Excellent, thanks!

I'll make a note of the site as well.
vass0922 is online now   Reply With Quote
Old July 24th, 2008, 03:08 PM     #6 (permalink)
Junior Member
 
Join Date: Jul 2008
Posts: 2
Thumbs up

They have some other code over at linqhelp.com too!
tremendo is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (532)
Misery Loves Company... (1849)
Why Does the MOON Grow Bigger as It.. (18)
heatsink issue (10)
New Mobo (18)
UPGRADING C/D DRIVE TO 250GB & .. (14)
1 internet. 1 house. 3 computer. ho.. (13)
SSD's, RAID, and External Backup (7)
Is This A Compatible Gaming PC? (18)
Recent Discussions
Building my first PC and need s.. (2)
32 or 64 bit vista (4)
Big problem with my PC (2)
system restore 'next' button wo.. (2)
firewall (1)
C++ compiler suggestions (4)
Official World of Warcraft Thre.. (4529)
Programming question (2)
UPGRADING C/D DRIVE TO 250GB &a.. (14)
FS: Dell 6000 laptop, modded 36.. (2)
Apple iPod touch 16 GB $200 (4)
Six 28-Disc Cross Design Black .. (4)


All times are GMT -4. The time now is 12:26 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28