Get selected files by using GetFiles does not work as expected

T

TonyJ

Hello!

I use this static method GetFiles from the Directory class and I hope to get
only those files that have file extension ini but I get
all files that begin with ini as the file extension for example *.iniQ and
*.iniP and so on.

Directory.GetFiles(Directory.GetCurrentDirectory(), "*.ini");

Is it possible to only get those for example have *.ini as in my case.



//Tony
 
A

Alberto Poblacion

TonyJ said:
I use this static method GetFiles from the Directory class and I hope to
get
only those files that have file extension ini but I get
all files that begin with ini as the file extension for example *.iniQ and
*.iniP and so on.

This probably happens because you are using the NTFS filesystem with the
default option that generates MS-DOS filenames from long filenames. You can
verify this from a command prompt, typing DIR /X. For instance, create al
file "test.inip" and do a DIR /X. You will get something similar to the
following:
22/11/2007 11:29 992 TEST~1.INI test.inip

This means that the same file is recognized by the system as either
"TEST~1.INI" or "test.inip". In fact, if you type "DIR *.ini" at the command
prompt, you will see "test.inip", because its alias of "TEST~1.INI" matched
the wildcard filter.

Directory.GetFiles(Directory.GetCurrentDirectory(), "*.ini");

Is it possible to only get those for example have *.ini as in my case.

As far as I know, Directory.GetFiles does not have an option to do this
filtering automatically. You will have to loop through the returned files in
your code, removing those that do not match your criteria.
 
J

Jon Skeet [C# MVP]

I use this static method GetFiles from the Directory class and I hope to get
only those files that have file extension ini but I get
all files that begin with ini as the file extension for example *.iniQ and
*.iniP and so on.

This is as documented:

<quote>
When using the asterisk wildcard character in a searchPattern, such as
"*.txt", the matching behavior when the extension is exactly three
characters long is different than when the extension is more or less
than three characters long. A searchPattern with a file extension of
exactly three characters returns files having an extension of three or
more characters, where the first three characters match the file
extension specified in the searchPattern. A searchPattern with a file
extension of one, two, or more than three characters returns only
files having extensions of exactly that length that match the file
extension specified in the searchPattern.
Is it possible to only get those for example have *.ini as in my case.

I suggest you call the method as you do now, and then filter out
anything with an extension of more than 3 characters. It's a pain, but
there we go...

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top