Directory.GetFiles () searchpattern

I have this base code

Directory.GetFiles(filepath, "*.OUT")

The file path contains a file with the name Filename.OUT

The above code detects that the file is in order.

Now I will rename the file, adding a unique identifier so that the file does not receive again.

Filename.OUT6F9619FF-8B86-D011-B42D-00C04FC964FF

However, the STILL file is picked up Directory.GetFiles()!

So, is the search pattern ( *.OUT) a regular expression pattern? If so, that makes sense.

MSDN information does not seem to mean this. If not, is there a pattern that I can use so that it doesn't fit?

+5
source share
3 answers

, . , , , :

bool ExtensionOf ( String f, String targetext ) {
    return f.EndsWith( targetext );
}

targetext, ".OUT"

!

+3

, , , :

SearchPattern , , , searchPattern.

:

SearchPattern , , , searchPattern.

, . , -. , .

+3

You can output a string (substring) along the length of guid:

string fileName = "Filename.OUT";
string seed = "6F9619FF-8B86-D011-B42D-00C04FC964FF";
string input = fileName + seed;
string result = input.Substring(0, input.Length - seed.Length);
FileInfo file = new FileInfo(result);
Console.WriteLine(file.Extension);

and work with the result, as usual:

private static string StripSeed(string input)
{
    return input.Substring(0, input.Length - 36); // length of seed (guid)
}

Directory.EnumerateFile("*.*")
         .Select(p => StripSeed(p))
         .Select(f => new FileInfo(f))
         .Where(fi => fi.Extesnsion.Equals(".OUT", StringComparison.OrdinalIgnoreCase));

Sandbox.

+2
source

All Articles