0

I am trying to verify if a file exist in a c# console program. The only thing is that the file can have any name.

The only thing that I know is the file extension and there can only be one file of this extension. How can I verify if it exist and then use it whatever the name is?

ESD
  • 675
  • 2
  • 12
  • 35
  • In PHP i would use glob() for matching filename. For example if your extension is .myext you could simply match *.myext and get a list of filenames. Then you can use the first. In this question somebody asked how to implement glob in C#: http://stackoverflow.com/questions/398518/how-to-implement-glob-in-c-sharp – Alex2php Dec 06 '12 at 01:08
  • Do you know the folder that the file will exist in? – wdavo Dec 06 '12 at 01:08
  • in the assignement the folder and filename can be specified by the user but there is still a default value for both of them – ESD Dec 06 '12 at 01:10
  • assignment folder? is this homework? we shouldn't be doing your homework for you. – John Gardner Dec 06 '12 at 01:15
  • the assignment is way more than just this. And also we have not seen anything on files before we got this assignment because we are in the end of what we call a "session" in our scholar system and there was one more graded assignement to be done. The final exams being next week we are on our own to figure it out about files. – ESD Dec 06 '12 at 01:18

4 Answers4

4

The problem with using Directory.GetFiles() is that is walks the entire filesystem first, then returns all matches as an array. Even if the very first file examined is the one and only match, it still walks the entire filesystem from the specified root before returning the one match.

Instead, use EnumerateFiles() to do a lazy walk, stopping when the first match is encountered, thus:

DirectoryInfo root        = new DirectoryInfo( @"C:\" ) ;
string        pattern     = "*.DesiredFileExtension" ;
FileInfo      desiredFile = root.EnumerateFiles( pattern , SearchOption.AllDirectories )
                                .First()
                                ;

It will throw an exception if the file's not found. Use FirstOrDefault() to get a null value instead.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
3

Try the Directory.GetFiles static method:

var fileMatches = Directory.GetFiles("folder to start search in", "*.exe", SearchOption.AllDirectories);
if (fileMatches.Length == 1)
{
    //my file was found
    //fileMatches[0] contains the path to my file
}

Note that with the SearchOption enum you can specify just the current folder or to search recursively.

slugster
  • 49,403
  • 14
  • 95
  • 145
1

Something like this may work

if (Directory.GetFiles(path, "*.ext").Any())
{
   var file = Directory.GetFiles(path, ".ext").First();
}
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • this requires an extra bit of code to actually get the file name (which is unknown - only the extension is known). – slugster Dec 06 '12 at 01:15
1
  string extension = "txt";
  string dir = @"C:\";

  var file = Directory.GetFiles(dir, "*." + extension).FirstOrDefault();

  if (file != null)
  {
    Console.WriteLine(file);
  }

If the file does not exist directly under 'dir', you will need to use SearchOption.AllDirectories for Directory.GetFiles

wdavo
  • 5,070
  • 1
  • 19
  • 20