Naveen's Weblog

Bridge to future

LINQ to system files and processes

Posted by codingsense on September 16, 2008

I was just thinking where and all linq can be used, surpirsingly i found that whatever i thought to do i was able to implement linq to make it much better.

Firstly i thought how linq can be used with file system and check what i got.

LINQ to System Files:

Here i made a sample which will search the files from system directory and list the files that are of .dll extension and have size greater than 100000 bytes

string DirPath = System.Environment.GetFolderPath(Environment.SpecialFolder.System);
string[] Files = (from file in new DirectoryInfo(DirPath).GetFiles()
                     where file.Extension == ".dll" && file.Length > 1000000
                     select file.Name).ToArray<string>();

Next i thought how linq can be used with processes

LINQ to system Processes:

Here we shall see how a running process can be handled using LINQ. This sample will list all the processes that are currently running and order them in ascending order of the memory taken

var ProcInfo = from proc in Process.GetProcesses()
       orderby proc.PagedMemorySize64
       select new { Name = proc.ProcessName , Memory = proc.PagedMemorySize64};

Download sample – 24 kb

So people just keep thinking where all you can utilize linq and you will get more creatives ways to use it.


Happy learing 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: