Most efficient way to find all exe files on disk using C#?
Asked Answered
R

1

1

What is the most efficient way to find all exe files on disk using C#?

It will be done in background thread in program so disk usage should be as small as possible.

Rudolf answered 9/6, 2012 at 23:20 Comment(4)
What have you done so far? You can enumerate all the directories of the machine and use the overload of Directory.GetFiles that takes a file pattern (e.g. ".exe"). If it is running, you might find qurying the Windows Search database to be much faster as it will have preindexed much of this info: msdn.microsoft.com/en-us/library/windows/desktop/…. There's no guarantee the Windows Search database is up to date, so simply enumerating all of the directories and and using Directory.GetFiles might be the most accurate, at least.Flacon
So far, msdn.microsoft.com/en-us/library/dd383458Rudolf
Note that the GetFiles/EnumerateFiles method is already covered here: #929776 - you can simply use a filter for exe files when searching a directory.Flacon
similar question answered here #724648Respect
I
6

The link you provided is the most efficient way in C# (with .Net 4.0): Directory.EnumerateFiles Method

Prior versions of .Net had to use a slower method that caused memory issues on large drives, @hatchet showed a great example: Is there a faster way to scan through a directory recursively in .NET?

I wouldn't suggest using the TPL as Jon Skeet mentions here: Task Parallel Library for directory traversal

If you see the first comment in this MSDN link: Iterate File Directories with the Parallel Class I dont even think Microsoft had success with this TPL method either.

The other suggestion I have is using LogParser and you can use it with C#! Its a free Microsoft product but I'm not sure about re-dist permissions, I had to include it in my package separately last time I used it. It full on flys, faster than a speeding train!

As per @spender comment I found a Log Parser example that finds files dated 180 days and older, you could try it out and adapt it if its useful:

SELECT
    ContentPath, [Days (Old)], FileName, [Creation Date Time]
    USING creationtime AS [Creation Date Time],
    TO_DATE([Creation Date Time]) AS Cdate,
    SUB(TO_LOCALTIME(SYSTEM_TIMESTAMP()), Cdate) AS Days,
    DIV(TO_INT(Days),86400) As [Days (Old)],
    EXTRACT_PATH(TO_LOWERCASE(path)) AS ContentPath,
    TO_LOWERCASE(name) AS FileName
FROM %source% 
WHERE
  (attributes NOT LIKE 'D%')
AND
  ([Days (Old)] >= TO_INT('%day%'))
ORDER BY [Creation Date Time] DESC

%source% could be something like c:\*.exe as shown in the argument c:\temp\*.*. Save the above as cc.sql, run it with the following syntax:

 C:\Temp\Tools\Logparser>LogParser.exe file:cc.sql?source="c:\temp\*.*"+day="180"  -i:FS -preserveLastAccTime -rtp:-1

EDIT

Thanks for the upvotes guys! I made this Event Analyser app in 2005 (before .net 2.0 was released) and since the Log Parser suggestion was so popular I thought I'd share the way you can use LogParser in .Net

The good folks over at http://visuallogparser.codeplex.com/ have provided us with the source code.

Open the VisualLogParser solution in VS2010, ignore the prompt about debugging, after the solution loads, F5, set the combo-box to FS (FileSystem), paste in this query and press go.

SELECT
    ContentPath, [Days (Old)], FileName, [Creation Date Time]
    USING creationtime AS [Creation Date Time],
    TO_DATE([Creation Date Time]) AS Cdate,
    SUB(TO_LOCALTIME(SYSTEM_TIMESTAMP()), Cdate) AS Days,
    DIV(TO_INT(Days),86400) As [Days (Old)],
    EXTRACT_PATH(TO_LOWERCASE(path)) AS ContentPath,
    TO_LOWERCASE(name) AS FileName
FROM 'c:\*.exe' 
WHERE
  (attributes NOT LIKE 'D%')
AND
  ([Days (Old)] >= TO_INT('180'))
ORDER BY [Creation Date Time] DESC

At the same time you may wish run any other .Net application that searches directories and compare the results!!!

Iconology answered 9/6, 2012 at 23:41 Comment(1)
It's not obvious how LogParser is relevant to the task of searching for .exe files.Dekameter

© 2022 - 2024 — McMap. All rights reserved.