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.
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
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.
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
Antistar
Use Path.GetFileNameWithoutExtension(string path) - (or something like that, it's under System.IO)
Related Shared Classes
File
Directory
mafandon
rivast_2001
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");