How to search for files by using only filenaems?

How to search for files on the drive by using only the filenames.ie i want to exclude file extensions while searching

Answer this question

How to search for files by using only filenaems?

  • clear_zero

    Yes, if you need the name without the extension that is what you need to do.

    You could use the DirectoryInfo.GetFiles() method to get a class with the extension already detected. It will give an array of FileInfo classes that you can extract more info from. As far as I can see you still get the full filename and have to remove the extension.

    Using DirectoryInfo.GetFiles() is most likely slower than Directory.GetFiles() because if gives an array of FileInfos instead of just names. Which one is best depends if more info from FileInfo is interesting for your application.



  • 52179apb

    Is there a way to get the file names excluding extensions. ie I want to match only the name of the file For example : if i have a file by name fileName.txt, i want to compare only 'fileName'.is there a way to get this result using Directory.GetFiles() or others
  • Antistar

    Use Path.GetFileNameWithoutExtension(string path) - (or something like that, it's under System.IO)

    Related Shared Classes

    File

    Directory

     

     


  • mafandon

    thanks.
  • rivast_2001

    so we cant get all names of files alone using Directory.GetFiles()..We have to manipulate them separately
  • jcnewbie

    If you count the extension as the last dot, if any, and what is after it you can do this.

    String strFilename = "test.txt";
    if (strFilename.LastIndexOf('.'
    ) >= 0)
    {
    strFilename = strFilename.Substring(0, strFilename.IndexOf(
    '.'
    ));
    }

    As you see, you have to do it for every file found. Maybe you can do the matching differently. You can then avoid changing the filename for matching which can be bad for performance.



  • Robert Reister

    You can use Directory.GetFiles(String, String) to get an array of strings with the filenames in a certain directory matching a search pattern.

    String[] arrFiles = Directory.GetFiles(@"c:\", "*.bat");



  • How to search for files by using only filenaems?