How to find file of certain string exists  
Author Message
bslim





PostPosted: Visual C# General, How to find file of certain string exists Top

Hi all,

I would like to find a file that have a string "test" in between the filename.

Can we use wildcard *test*.* in c#

If not , may I know how to do it

Thanks



Visual C#12  
 
 
bslim





PostPosted: Visual C# General, How to find file of certain string exists Top

Hi,

I had a file "123test123.txt" and I tried this. But it doesn't work. Please help. Thanks

string[] file = Directory.GetFiles(pathDataOut);

for (int i=0; i<file.Length; i++)

if (file.ToString().Contains("test"))

{

Label4.Text = "found";

}


 
 
Geert Verhoeven





PostPosted: Visual C# General, How to find file of certain string exists Top

Hi,

You need to use the Directory class from the System.IO namespace.
Example:

string[] files = System.IO.Directory , "*test*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
Console.WriteLine(file);
}

Note: The pattern is not case sensitive.

Greetz,

Geert

Geert Verhoeven
Ausy Belgium

My Personal Blog