-1

I am trying to get the most recent file from a directory that also has a certain prefix. I am able to successfully use the code if I don't put the search string overload in after getfiles(), but if I do use it, I get an exception stating:

An unhandled exception of type 'System.InvalidOperationException' has occurred in System.Core.dll

FileInfo mostrecentlog = (from f in logDirectory.GetFiles("Receive") orderby f.LastWriteTime descending select f).First();
user3494110
  • 417
  • 2
  • 9
  • 25
  • And what does the message of the InvalidOperationException say? It's not documented but maybe it doesn't like search patterns with no wildcards. For prefixes you'd want `"Receive*"` – Matti Virkkunen Apr 26 '16 at 14:33
  • What is your search pattern? You really should show the code that doesn't work instead of something that does. – juharr Apr 26 '16 at 14:34
  • you should be able to simply add a where condition to your linq to search for the specific files, or if you want, simply pull back the full list and iterate through them (i'd recommend the first of these two suggestions personally). – user2366842 Apr 26 '16 at 14:34
  • 1
    The exception message contains ***EVERYTHING*** you need to solve this problem. *Your* problem is that you're not looking at it. Either catch it and call `ToString` on it, or debug it and use the "copy exception details to the clipboard" link on the exception helper dialog. We can't help you without this information, all we can do is guess. Go forth, get the exception details, and past them into an [edit]. –  Apr 26 '16 at 14:37

3 Answers3

2

Well, you need to ask few questions to yourself.

What if there is no matching file? Will your current implementation work? no. it will crash. Why? because of .First() operator.

As you mentioned, you want to find files with certain prefix, so add wildcard * to your prefix. Find all files starting with some prefix.

FileInfo mostrecentlog = (from f in logDirectory.GetFiles("your_pattern*") orderby
                                      f.LastWriteTime descending select f).FirstOrDefault();

Now check if mostrecentlog is not null, if it's not null then it will contain your most recent file matching certain prefix.

Saleem
  • 8,728
  • 2
  • 20
  • 34
1

Using method syntax might make this read a bit easier along with a Where() clause to specify what you are searching for:

// You must specify the path you want to search ({your-path}) when using the GetFiles()
// method.
var mostRecentFile = logDirectory.GetFiles("{your-path}")
                                 .Where(f => f.Name.StartsWith("Receive"))
                                 .OrderByDescending(f => f.LastWriteTime)
                                 .FirstOrDefault();

Likewise, you can specify a search pattern within the Directory.GetFiles() method as a second parameter :

// You can supply a path to search and a search string that includes wildcards
// to search for files within the specified directory
var mostRecentFile = logDirectory.GetFiles("{your-path}","Receive*")
                                 .OrderByDescending(f => f.LastWriteTime)
                                 .FirstOrDefault();

It's important to remember that FirstOrDefault() will return the first element that is found or null if no items are found, so you'll need to perform a check to ensure that you found something before continuing :

// Get your most recent file
var mostRecentFile = DoTheThingAbove();
if(mostRecentFile != null)
{
      // A file was found, do your thing.
}
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

Just use FirstOrDefault() instead of First()

FileInfo mostrecentlog = (from f in logDirectory.GetFiles("Receive") orderby f.LastWriteTime descending select f).FirstOrDefault()
mayowa ogundele
  • 475
  • 2
  • 6
  • 19